序列化对象顺序

时间:2016-02-04 12:06:27

标签: c# serialization

我正在尝试使用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>

1 个答案:

答案 0 :(得分:1)

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

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的更多信息,请访问here

在使用属性时也要考虑此约定。注意来自this页。

  

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