C# xml 反序列化不会将子元素提取到列表中

Posted

技术标签:

【中文标题】C# xml 反序列化不会将子元素提取到列表中【英文标题】:C# xml deserialization won't pick up child elements into list 【发布时间】:2021-03-18 03:39:53 【问题描述】:

我不知道为什么这不起作用,我搜索了答案,当我总结所有这应该起作用但不知何故它不起作用。所以我有一个 Bill 类,它有一些可以很好地反序列化的属性,但是我有属性 List items 是空的。

比尔课的过去

[Serializable()]
[XmlRoot("Bills")]
public class Bill

    private DateTime dateTimePrivate;
    
    **[XmlArray("Bill")]
    [XmlArrayItem("Article", typeof(Article))]
    public List<Article> articles  get; set; **
    [XmlAttribute("User")]
    public string user  get; set;  //Username
    [XmlAttribute("Total")]
    public int totalPrice  get; set; 
    [XmlAttribute("ID")]
    public int id  get; set; 

文章类

[Serializable()]
[XmlRoot("Article")]
public class Article

    [XmlAttribute("ID")]
    public int ID  get; set; 
    [XmlAttribute("name")]
    public string name  get; set; 
    [XmlAttribute("buttonName")]
    public string buttonName  get; set; 
    [XmlAttribute("price")]
    public float price  get; set; 
    [XmlAttribute("quantity")]
    public float quantity  get; set;  //Namijenjeno samo za racune
    [XmlAttribute("totalPrice")]
    public float totalPrice  get  return price * quantity;  set  totalPrice = price * quantity;  

AllBIlls.xml

<?xml version="1.0" encoding="utf-8"?>
<BillsCollection>
   <Bills>
      <Bill ID="0" Time="06-12-2020 13:28" User="TEST" SavedToDatabase="TEST" Total="24">
        <Article ID="0" name="CocaCola" quantity="1" price="12" totalPrice="12" />
        <Article ID="1" name="Sprite" quantity="1" price="12" totalPrice="12" />
    </Bill>
    <Bill ID="1" Time="06-12-2020 13:28" User="TEST" SavedToDatabase="TEST" Total="36">
        <Article ID="0" name="CocaCola" quantity="2" price="12" totalPrice="24" />
        <Article ID="1" name="Sprite" quantity="1" price="12" totalPrice="12" />
    </Bill>
 </Bills>
</BillsCollection>

反序列化

public static BillList GetBills()
    
        Console.WriteLine("reading bills");

        BillList allBills = null;

        //Adding root element for serialziation
        XmlRootAttribute xRoot = new XmlRootAttribute();
        xRoot.ElementName = "BillsCollection";
        xRoot.IsNullable = true;

        XmlSerializer serializer = new XmlSerializer(typeof(BillList), xRoot);
        XmlReader reader = AllBillsXml.CreateReader();

        allBills = (BillList)serializer.Deserialize(reader);
        reader.Close();

        Console.WriteLine("reading succesful");

        return allBills;
    

【问题讨论】:

【参考方案1】:

我使用了这个网站https://json2csharp.com/xml-to-csharp 以及您提供的 xml,我可以很好地反序列化 Articles 集合。

这里是生成的类:

// using System.Xml.Serialization;
    // XmlSerializer serializer = new XmlSerializer(typeof(BillsCollection));
    // using (StringReader reader = new StringReader(xml))
    // 
    //    var test = (BillsCollection)serializer.Deserialize(reader);
    // 

    [XmlRoot(ElementName = "Article")]
    public class Article
    

        [XmlAttribute(AttributeName = "ID")]
        public String ID  get; set; 

        [XmlAttribute(AttributeName = "name")]
        public String Name  get; set; 

        [XmlAttribute(AttributeName = "quantity")]
        public String Quantity  get; set; 

        [XmlAttribute(AttributeName = "price")]
        public String Price  get; set; 

        [XmlAttribute(AttributeName = "totalPrice")]
        public String TotalPrice  get; set; 
    

    [XmlRoot(ElementName = "Bill")]
    public class Bill
    

        [XmlElement(ElementName = "Article")]
        public List<Article> Article  get; set; 

        [XmlAttribute(AttributeName = "ID")]
        public String ID  get; set; 

        [XmlAttribute(AttributeName = "Time")]
        public String Time  get; set; 

        [XmlAttribute(AttributeName = "User")]
        public String User  get; set; 

        [XmlAttribute(AttributeName = "SavedToDatabase")]
        public String SavedToDatabase  get; set; 

        [XmlAttribute(AttributeName = "Total")]
        public String Total  get; set; 
    

    [XmlRoot(ElementName = "Bills")]
    public class Bills
    

        [XmlElement(ElementName = "Bill")]
        public List<Bill> Bill  get; set; 
    

    [XmlRoot(ElementName = "BillsCollection")]
    public class BillsCollection
    

        [XmlElement(ElementName = "Bills")]
        public Bills Bills  get; set; 
    

这里是填充的 Articles 对象:

这是反序列化代码:

           XmlSerializer serializer = new XmlSerializer(typeof(BillsCollection));

            using (StreamReader reader = new StreamReader(@"Path to xml.xml"))
            
                var test = (BillsCollection)serializer.Deserialize(reader);
            

使用“System.Xml.Serialization”命名空间

【讨论】:

感谢您提供的代码和一些修改我得到了我想要的【参考方案2】:

您是否尝试过使用 XmlTextReader 而不是 XmlReader?这可能会对您有所帮助。

【讨论】:

以上是关于C# xml 反序列化不会将子元素提取到列表中的主要内容,如果未能解决你的问题,请参考以下文章

在 C# 中反序列化 XML [重复]

如何在 C# 中反序列化具有属性的 XML [重复]

如何在 C# 中反序列化具有前缀的 XML

C# Restful WCF 服务。无法在帖子正文中反序列化 XML

如何反序列化 C# 中只有属性的 xml 元素?

如何在 .NET 中反序列化为本地集合?