c# - 将具有多个 namespace 的 XML 反序列化为对象

标签 c# xml deserialization

我正在尝试将此 XML 反序列化为 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>

代码:

我没有收到任何“项目”列表。该对象未反序列化。

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

最佳答案

您有几个问题:

  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 时,我确实很好地填充了对象。

关于c# - 将具有多个 namespace 的 XML 反序列化为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15441030/

相关文章:

android - 如何创建具有最小和最大高度的布局?

c# - C#中的序列化和Java中的反序列化

json - 在 GO 中反序列化一个非标准的 json

c++ - Cereal 二进制文件序列化/反序列化

c# - 组织 WinForm 控件代码

c# - System.Version 未序列化

c# - TPL 数据流本地存储或类似的东西

android - match_parent 不工作 xml android

c# - .NET 在第 3 方应用程序中获取所有异常

c# - DllImport - C 类型到 .NET 类型