Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • webmonger 130 posts 285 karma points
    Aug 11, 2010 @ 12:07
    webmonger
    0

    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

    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.

    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.

    StringWriter outStream = new StringWriter();
    JsonSerializer jsonSerializer = JsonSerializer.Create(new JsonSerializerSettings());
    jsonSerializer.Serialize(outStream, new Course() { Name = "Bob", MainTitle = "Blah" });
    return outStream.ToString();

    Cheers

    Jon

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Aug 11, 2010 @ 12:17
    Matt Brailsford
    1

    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.

    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();

        var xml = XElement.Parse(sb.ToString());


        return xml.CreateNavigator();
    }

    This should give you enough info to get started.

    Matt

  • webmonger 130 posts 285 karma points
    Aug 11, 2010 @ 12:29
    webmonger
    0

    That worked perfectly Matt cheers :)

    I just swapped

    return xml.CreateNavigator();

    for

    return sb.ToString();

    nice one :)

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Aug 11, 2010 @ 13:56
    Aaron Powell
    0

    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

  • webmonger 130 posts 285 karma points
    Aug 11, 2010 @ 17:44
    webmonger
    0

    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

    [DataMember]

    Attribute to all fields in all classes and they are now displayed in the XML output.

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Aug 11, 2010 @ 17:53
    Matt Brailsford
    0

    Sweet. Glad you got it sorted.

    I hope you are using AutoExport2DotNet?

    Matt

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Aug 12, 2010 @ 00:37
    Aaron Powell
    0

    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.

  • webmonger 130 posts 285 karma points
    Aug 12, 2010 @ 09:14
    webmonger
    0

    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

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Aug 12, 2010 @ 09:17
    Aaron Powell
    0

    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.

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Aug 12, 2010 @ 09:27
    Matt Brailsford
    0

    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

  • webmonger 130 posts 285 karma points
    Aug 12, 2010 @ 10:06
    webmonger
    0

    Cheers guys.

    I'll investigate further.

    Jon

Please Sign in or register to post replies

Write your reply to:

Draft