4

I´m trying to deserialize this XML to objects in C# .NET 4.5:

<DIDL-Lite xmlns:dc="http://purl.org/dc/elements/1.1/"
       xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/"
       xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/">
    <item id="28" parentID="19" restricted="1">
        <dc:creator>Alicia Keys</dc:creator> 
        <dc:date>2003-01-01</dc:date>
        <dc:title>Gangsta Lovin&apos; (feat. Alicia Keys)</dc:title>
    </item>
</DIDL-Lite>

Code:

I´m not getting any "item" Lists. The object isn't deserialized.

MemoryStream reader = new MemmoryStream(System.Text.Encoding.Unicode.GetBytes(Result));
var ser = new XmlSerializer(typeof(DIDLLite));
DIDLLite device = (DIDLLite)ser.Deserialize(reader);

Class DIDLLite:

[XmlRoot("DIDL-Lite", Namespace = "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/")]
public class DIDLLite {
    DIDLLite() {
        this.serviceItem = new List<ContainerItem>();
    }

    [System.Xml.Serialization.XmlArrayItem("item", typeof(ServiceListTypeService), IsNullable = false)]
    List<ContainerItem> serviceItem = new List<ContainerItem>();
}      

Class ContainerItem:

public class ContainerItem
{
    [System.Xml.Serialization.XmlAttribute("id")]
    public string id { get; set; }

    [System.Xml.Serialization.XmlAttribute("parentID")]
    public string parentID { get; set; }

    [System.Xml.Serialization.XmlAttribute("restricted")]
    public string restricted { get; set; }

    [System.Xml.Serialization.XmlAttribute("searchable")]
    public string searchable { get; set; }

    public string title { get; set; }
}
1
  • I think you need to add in the namespaces to the NamespaceManager as well, and then feed it into the XmlSerializer. I'm just recollecting this off the top of my head, will post a full answer once I remember it all. Commented Mar 15, 2013 at 20:17

1 Answer 1

3

You have several issues:

  1. you define an XmlArrayItem attribute - but really, in your XML, you don't have any list of items. If you want to use an Xml array construct, you'd need to have something like this for your XML:

    <DIDL-Lite .....>
        <Items>
           <item id="28" parentID="19" restricted="1">
            ......
           </item>
           <item id="29" parentID="19" restricted="1">
            ......
           </item>
        </Items>
    </DIDL-Lite>
    

    So you would need to have an <Items>...</Items> wrapper around your <item> elements.

  2. You have this declaration:

    [XmlArrayItem("item", typeof(ServiceListTypeService), IsNullable = false)]
    

    but where is the ServiceListtypeService defined? I don't see any trace of it....

I simplified your code a bit - and this works just fine:

[XmlRoot("DIDL-Lite", Namespace = "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/")]
public class DIDLLite
{
    [XmlElement("item")]
    public ContainerItem Item { get; set; }
}


public class ContainerItem
{
    [XmlAttribute("id")]
    public string id { get; set; }

    [XmlAttribute("parentID")]
    public string parentID { get; set; }

    [XmlAttribute("restricted")]
    public string restricted { get; set; }

    [XmlAttribute("searchable")]
    public string searchable { get; set; }

    // you were missing these elements and their namespace

    [XmlElement(Namespace = "http://purl.org/dc/elements/1.1/")]
    public string creator { get; set; }
    [XmlElement(Namespace = "http://purl.org/dc/elements/1.1/")]
    public string date { get; set; }
    [XmlElement(Namespace = "http://purl.org/dc/elements/1.1/")]
    public string title { get; set; }
}

And now, when I run your code to deserialize your XML, I do get the objects filled nicely.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This was driving me crazy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.