c# – 将XML反序列化为对象时缺少子节点

前端之家收集整理的这篇文章主要介绍了c# – 将XML反序列化为对象时缺少子节点前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要处理某些xml,无法从中反序列化对象列表.以下是xml:
<catalog>
<item>
    <id>18338517</id>
    <note label="Name ">Gear xyz</note>
    <note label="Size ">10</note>       
    <note label="Source">Store xyz</note>
    <relation weight="100">
        <type>External</type>
        <id>123</id>
        <name>Mcday</name>          
    </relation>
    <relation weight="99">
        <type>Internal</type>
        <id>234</id>
        <name>Mcnight</name>
    </relation>
</item>
    <item> ..... </item></catalog>

以下是我的课

[XmlRoot("catalog")]
public class Catalog
{
    [XmlArray("item")]
    [XmlArrayItem("item",typeof(Item))]
    public Item[] item{ get; set; }
}

[XmlRoot("item")]
public class Item
{
    [XmlElement("id")]
    public string id { get; set; }

    [XmlArrayItem("note",typeof(Note))]
    public Note[] note { get; set; }

    [XmlArrayItem("relation",typeof(Relation))]
    public Relation[] relation { get; set; }
}

[Serializable]
public class Note
{
    [XmlAttribute("label")]
    public string label { get; set; }

    [XmlText]
    public string Value { get; set; }
}

[Serializable]
public class Relation
{
    [XmlAttribute("weight")]
    public string weight { get; set; }

    [XmlText]
    public string Value { get; set; }

    [XmlElement("id")]
    public string id { get; set; }

    [XmlElement("type")]
    public string type { get; set; }

    [XmlElement("name")]
    public string name { get; set; }
}

最后,要求反序列化

Catalog catalog = null;
XmlSerializer mySerializer = new XmlSerializer(typeof(Catalog));
using (TextReader reader = new StreamReader(@"D:\TEMP\deserialize xml\catalog.xml"))
{
    catalog = (Catalog)mySerializer.Deserialize(reader);
}

它不会返回任何错误,只是目录对象中的空项.

解决方法

要像在此处一样使用XmlArray和XmlArrayItem属性
[XmlArray("term")]
[XmlArrayItem("term",typeof(Item))]
public Item[] item{ get; set; }

您的XML必须实际提供正确的集合元素.所以你的课应该是这样的

[XmlRoot("catalog")]
public class Catalog
{
    [XmlArray("items")] // note that I corrected 'term' to 'items'/'item'
    [XmlArrayItem("item",typeof(Item))]
    public Item[] item{ get; set; }
}

你的XML应该是这样的

<catalog>
  <items>
    <item>
      <id>18338517</id>
       ...

请注意< items>对应于[XmlArray(“items”)]的元素,以及子元素< item>对应于[XmlArrayItem(“item”,typeof(Item))].

如果您不想/不能更改XML格式,请使用XmlElement属性而不是XmlArrayItem属性.所以你的最终代码应如下所示:

[XmlRoot("catalog")]
public class Catalog
{
     [XmlElement("item")] // no XmlArray/XmlArrayItem,just XmlElement
     public Item[] Items { get; set; }
}

[XmlType("item")]
public class Item
{
    [XmlElement("id")]
    public string id { get; set; }

    [XmlElement("note",typeof(Note))] // no XmlArray/XmlArrayItem,just XmlElement
    public Note[] note { get; set; }

    [XmlElement("relation",typeof(Relation))] // no XmlArray/XmlArrayItem,just XmlElement
    public Relation[] relation { get; set; }
}

[Serializable]
public class Note
{
    [XmlAttribute("label")]
    public string label { get; set; }

    [XmlText]
    public string Value { get; set; }
}

[Serializable]
public class Relation
{
    [XmlAttribute("weight")]
    public string weight { get; set; }

    [XmlText]
    public string Value { get; set; }

    [XmlElement("id")]
    public string id { get; set; }

    [XmlElement("type")]
    public string type { get; set; }

    [XmlElement("name")]
    public string name { get; set; }
}
原文链接:https://www.f2er.com/csharp/92038.html

猜你在找的C#相关文章