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

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

以下是我的课

  1. [XmlRoot("catalog")]
  2. public class Catalog
  3. {
  4. [XmlArray("item")]
  5. [XmlArrayItem("item",typeof(Item))]
  6. public Item[] item{ get; set; }
  7. }
  8.  
  9. [XmlRoot("item")]
  10. public class Item
  11. {
  12. [XmlElement("id")]
  13. public string id { get; set; }
  14.  
  15. [XmlArrayItem("note",typeof(Note))]
  16. public Note[] note { get; set; }
  17.  
  18. [XmlArrayItem("relation",typeof(Relation))]
  19. public Relation[] relation { get; set; }
  20. }
  21.  
  22. [Serializable]
  23. public class Note
  24. {
  25. [XmlAttribute("label")]
  26. public string label { get; set; }
  27.  
  28. [XmlText]
  29. public string Value { get; set; }
  30. }
  31.  
  32. [Serializable]
  33. public class Relation
  34. {
  35. [XmlAttribute("weight")]
  36. public string weight { get; set; }
  37.  
  38. [XmlText]
  39. public string Value { get; set; }
  40.  
  41. [XmlElement("id")]
  42. public string id { get; set; }
  43.  
  44. [XmlElement("type")]
  45. public string type { get; set; }
  46.  
  47. [XmlElement("name")]
  48. public string name { get; set; }
  49. }

最后,要求反序列化

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

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

解决方法

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

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

  1. [XmlRoot("catalog")]
  2. public class Catalog
  3. {
  4. [XmlArray("items")] // note that I corrected 'term' to 'items'/'item'
  5. [XmlArrayItem("item",typeof(Item))]
  6. public Item[] item{ get; set; }
  7. }

你的XML应该是这样的

  1. <catalog>
  2. <items>
  3. <item>
  4. <id>18338517</id>
  5. ...

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

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

  1. [XmlRoot("catalog")]
  2. public class Catalog
  3. {
  4. [XmlElement("item")] // no XmlArray/XmlArrayItem,just XmlElement
  5. public Item[] Items { get; set; }
  6. }
  7.  
  8. [XmlType("item")]
  9. public class Item
  10. {
  11. [XmlElement("id")]
  12. public string id { get; set; }
  13.  
  14. [XmlElement("note",typeof(Note))] // no XmlArray/XmlArrayItem,just XmlElement
  15. public Note[] note { get; set; }
  16.  
  17. [XmlElement("relation",typeof(Relation))] // no XmlArray/XmlArrayItem,just XmlElement
  18. public Relation[] relation { get; set; }
  19. }
  20.  
  21. [Serializable]
  22. public class Note
  23. {
  24. [XmlAttribute("label")]
  25. public string label { get; set; }
  26.  
  27. [XmlText]
  28. public string Value { get; set; }
  29. }
  30.  
  31. [Serializable]
  32. public class Relation
  33. {
  34. [XmlAttribute("weight")]
  35. public string weight { get; set; }
  36.  
  37. [XmlText]
  38. public string Value { get; set; }
  39.  
  40. [XmlElement("id")]
  41. public string id { get; set; }
  42.  
  43. [XmlElement("type")]
  44. public string type { get; set; }
  45.  
  46. [XmlElement("name")]
  47. public string name { get; set; }
  48. }

猜你在找的C#相关文章