I'm trying out LINQ to Umbraco for the first time.
I'm using Umbraco's REST interface to send an Id to the class to pull back XML data based on a LINQ to Umbraco class. A simplified version is below
StringWriter outStream = new StringWriter(); XmlSerializer serializer = new XmlSerializer(typeof(Course)); serializer.Serialize(outStream, new Course() { Name = "Bob", MainTitle = "Blah" }); return outStream.ToString();
The problem is when i try to do the "serializer.Serialize" I get an error:
INNEREXCEPTION: System.InvalidOperationException: There was an error reflecting type 'eCMT.Core.Code.UmbracoData.Course'. ---> System.InvalidOperationException: To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object) at all levels of their inheritance hierarchy.
I've not tried it with Linq to Umbraco yet, but this is how I serialise a Linq to Sql entity in my Private Messaging package which uses the DataContract attribute.
public static XPathNavigator ToXml<T>(this T obj) { DataContractSerializer dcs = new DataContractSerializer(typeof(T)); StringBuilder sb = new StringBuilder(); XmlWriter writer = XmlWriter.Create(sb); dcs.WriteObject(writer, obj); writer.Close();
You'll be happy to know that I've fixed this serialization problem for the next version. I must have missed it when I did a copy across to the new generation engine.
When i Serialize node X i want to serialize all of it's children as well but it does not seem to loop through the children at the moment. All it outputs is the object that i've input. I need recursive serialization any ideas?
no idea, that'll be something to do with how the DataContractSerializer(s) work, I don't know how they handle complex object/ collection object serialization.
That should work as long as all your classes have the [DataContract] attribute and all your methods have the [DataMember] attribute. In my private messaging package, that serializes an entity with a List of other entities and they all get serialized fine.
Linq to Umbraco xml serialization
Hi.
I'm trying out LINQ to Umbraco for the first time.
I'm using Umbraco's REST interface to send an Id to the class to pull back XML data based on a LINQ to Umbraco class. A simplified version is below
The problem is when i try to do the "serializer.Serialize" I get an error:
INNEREXCEPTION:
System.InvalidOperationException: There was an error reflecting type 'eCMT.Core.Code.UmbracoData.Course'. ---> System.InvalidOperationException: To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object) at all levels of their inheritance hierarchy.
In the docs at http://our.umbraco.org/wiki/reference/api-cheatsheet/linq-to-umbraco/understanding-the-generated-classes it says the classes have been setup to use System.Runtime.Serialization
Could some one explain what i'm doing wrong?
PS. I managed to get JSON.net to convert the object to JSON first try so the objects can be enumerated.
Cheers
Jon
Hey Jon,
I've not tried it with Linq to Umbraco yet, but this is how I serialise a Linq to Sql entity in my Private Messaging package which uses the DataContract attribute.
This should give you enough info to get started.
Matt
That worked perfectly Matt cheers :)
I just swapped
for
nice one :)
Yep LINQ to Umbraco is designed to use the DataContractSerializer types, so it can go to JSON, XML, etc and you can pass it around with WCF easily
Turns out there was a touch more work to do.
I was able to get the Base Data out using the DataContractDerializer but non of my custom fields came out.
I added
Attribute to all fields in all classes and they are now displayed in the XML output.
Sweet. Glad you got it sorted.
I hope you are using AutoExport2DotNet?
Matt
You'll be happy to know that I've fixed this serialization problem for the next version. I must have missed it when I did a copy across to the new generation engine.
Cheers Slace.
One final question. ;)
When i Serialize node X i want to serialize all of it's children as well but it does not seem to loop through the children at the moment. All it outputs is the object that i've input. I need recursive serialization any ideas?
Cheers
Jon
no idea, that'll be something to do with how the DataContractSerializer(s) work, I don't know how they handle complex object/ collection object serialization.
Hey Jon,
That should work as long as all your classes have the [DataContract] attribute and all your methods have the [DataMember] attribute. In my private messaging package, that serializes an entity with a List of other entities and they all get serialized fine.
Matt
Cheers guys.
I'll investigate further.
Jon
is working on a reply...