2013-03-15 53 views
4

我正尝试反序列化这个XML对象在C#.NET 4.5:反序列化XML和多个命名空间的对象

<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> 

代码:

我可不是得到任何 “项目” 列表。该对象不是反序列化的。

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

DIDLLiteContainerItem

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

类:

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; } 
} 
+0

我想你还需要在名称空间中添加NamespaceManager,然后将它提供给XmlSerializer。我只是从头脑中回忆起这一点,一旦我记住了这一切,就会发布一个完整的答案。 – code4life 2013-03-15 20:17:54

回答

3

你有几个问题:

  1. 你定义一个XmlArrayItem属性 - 卜实际上,在您的XML中,您没有任何项目列表。如果你想使用XML数组结构,你需要有这样的事情为XML:

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

    所以,你需要有在你<item>元素的<Items>...</Items>包装。

  2. 你有这样的声明:

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

    但如果是ServiceListtypeService界定?我没有看到它的任何痕迹....

我简化你的代码位 - 这工作得很好:现在

[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; } 
} 

而且,当我运行您的代码进行反序列化你的XML,我确实得到了很好的对象。

+0

谢谢!这让我疯狂。 – 2013-03-15 21:06:01

相关问题