4

このXMLをC#.NET4.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>

コード:

「アイテム」リストが表示されません。オブジェクトは逆シリアル化されません。

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

クラス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>();
}      

クラス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; }
}
4

1 に答える 1

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>
    

    したがって、要素の<Items>...</Items>周りにラッパーが必要になります。<item>

  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を逆シリアル化するとき、私はオブジェクトをうまく埋めさせます。

于 2013-03-15T20:39:50.050 に答える