序列化对象顺序

本文关键字:顺序 对象 序列化 | 更新日期: 2023-09-27 18:33:09

我正在尝试在 C# 中序列化一个类,现在它工作正常,问题是我希望以特定顺序进行 xml 序列化并尝试使用 [XmlElementAttribute(Order = 1(]

但是这样做我的程序开始在执行时出错,如果我删除上述语句,但随后我没有以所需的方式排序元素,则可以正常工作。

错误:"出现反映类型'WindowsFormsApplication1.Bill'的错误。

法典

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace WindowsFormsApplication1
{
    [Serializable()]
    [System.Xml.Serialization.XmlRoot(ElementName = "Bill", Namespace = "", IsNullable = false)]
    public class Bill 
    {
        [System.Xml.Serialization.XmlElement("DocNo")]
        [XmlElementAttribute(Order = 1)] 
        public string DocNo { get; set; }
        [System.Xml.Serialization.XmlElement("DocDt")]
        [XmlElementAttribute(Order = 2)] 
        public string DocDt { get; set; }
        [XmlArray("Bill-Items")]
        [XmlElementAttribute(Order = 3)] 
        public List<Item> items = new List<Item>();
        [System.Xml.Serialization.XmlElement("DocAmount")]
        [XmlElementAttribute(Order = 4)] 
        public string DocAmount{ get; set; }

        public void Serialize()
        {
            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(this.GetType());
            x.Serialize(Console.Out, this);
        }
        public void Deserialize()
        {
            BillsCollection bills = null;
            string path = "E:''data.xml";
            XmlSerializer serializer = new XmlSerializer(typeof(BillsCollection));
            StreamReader reader = new StreamReader(path);
            bills = (BillsCollection)serializer.Deserialize(reader);
            reader.Close();
        }

        public Bill()
        {
            items.Add(new Item { StockNo = "2323", MRP=234.34,Qty=2 });
            items.Add(new Item { StockNo = "4564", MRP = 1250.00, Qty = 5 });
        }
    }
    [Serializable()]
    [System.Xml.Serialization.XmlRoot("Item")]
    public class Item
    {
        public string StockNo;
        public double MRP;
        public double Qty;
        public string TaxCode;
        public double TaxAmount;
        public double TaxPercent;
        public string DiscCode;
        public double DiscAmount;
        public double DiscPer;
    }
    public class BillItems
    {
    //    [XmlArray]
    }

    [Serializable()]
    [System.Xml.Serialization.XmlRoot(ElementName="Bills",Namespace="", IsNullable=false)]
    public class BillsCollection
    {
        [XmlArray("Bills")]
        [XmlArrayItem("Bill", typeof(Bill))]
        public Bill[] Bills { get; set; }
        public static void Add(Bill bill )
        {
        }
    }

}
Current Output
---------------
<Bill xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Bill-Items>
    <Item>
      <StockNo>2323</StockNo>
      <MRP>234.34</MRP>
      <Qty>2</Qty>
      <TaxAmount>0</TaxAmount>
      <TaxPercent>0</TaxPercent>
      <DiscAmount>0</DiscAmount>
      <DiscPer>0</DiscPer>
    </Item>
    <Item>
      <StockNo>4564</StockNo>
      <MRP>1250</MRP>
      <Qty>5</Qty>
      <TaxAmount>0</TaxAmount>
      <TaxPercent>0</TaxPercent>
      <DiscAmount>0</DiscAmount>
      <DiscPer>0</DiscPer>
    </Item>
  </Bill-Items>
  <DocNo>1</DocNo>
  <DocAmount>2001</DocAmount>
</Bill>

Required Output (Basically Doc No and Doc Amount should be before Item List 
---------------
<Bill xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Bill-Items>
  <DocNo>1</DocNo>
  <DocAmount>2001</DocAmount>
    <Item>
      <StockNo>2323</StockNo>
      <MRP>234.34</MRP>
      <Qty>2</Qty>
      <TaxAmount>0</TaxAmount>
      <TaxPercent>0</TaxPercent>
      <DiscAmount>0</DiscAmount>
      <DiscPer>0</DiscPer>
    </Item>
    <Item>
      <StockNo>4564</StockNo>
      <MRP>1250</MRP>
      <Qty>5</Qty>
      <TaxAmount>0</TaxAmount>
      <TaxPercent>0</TaxPercent>
      <DiscAmount>0</DiscAmount>
      <DiscPer>0</DiscPer>
    </Item>
  </Bill-Items>
</Bill>

序列化对象顺序

您两次使用了相同的属性:

public class Bill 
{
    [System.Xml.Serialization.XmlElement("DocNo")]
    [XmlElementAttribute(Order = 1)] 
    public string DocNo { get; set; }
    ......
}

将它们更改为:

public class Bill 
{
    [System.Xml.Serialization.XmlElement(ElementName = "DocNo", Order = 1)] 
    public string DocNo { get; set; }
    ......
}

有关 XmlElementAttribute 的详细信息可以在此处找到。

在使用属性时,还要考虑此约定。注意来自此页面。

注意:按照惯例,所有属性名称都以单词结尾 "属性",用于将它们与 .NET 中的其他项区分开来 框架。但是,您不需要指定属性后缀 在代码中使用属性时。