【问题标题】:Missing child nodes when deserializing XML to object将 XML 反序列化为对象时缺少子节点
【发布时间】:2014-04-01 09:30:54
【问题描述】:

我需要处理某些 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);
}

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

【问题讨论】:

    标签: c# .net xml


    【解决方案1】:

    要使用 XmlArrayXmlArrayItem 属性,就像您在此处一样:

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

    注意&lt;items&gt; 元素对应于[XmlArray("items")],子元素&lt;item&gt; 对应于[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; }
    }
    

    【讨论】:

    • 我尝试更改课程,它就像魔术一样工作:) 非常感谢您的帮助
    【解决方案2】:

    你的属性是错误的:

    public class Catalog
    {
        [XmlArray("term")]
        [XmlArrayItem("term", typeof(Item))]
    

    试试这个:

    public class Catalog
    {
        [XmlArray("items")]
        [XmlArrayItem("item", typeof(Item))]
    

    编辑:为了进一步开发您的项目,您应该考虑创建一个 XML 架构并生成代码,例如使用 XSD.exe,请参阅此问题:@ 987654321@

    EDIT2:查看 MSDN,XmlArrayAttribute 构造函数定义为:

    public XmlArrayAttribute(
        string elementName
    )
    

    参数记录为:

    元素名称 类型:System.String

    The name of the XML element that the XmlSerializer generates.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-10
      • 1970-01-01
      • 1970-01-01
      • 2014-11-30
      • 1970-01-01
      相关资源
      最近更新 更多