2013-03-15 78 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

相關問題