c#serialize嵌套类

时间:2013-09-27 11:46:14

标签: c# xml serialization

我正在尝试序列化一系列虚拟订单,其中每个订单都包含一个产品。该集合正在序列化,但订单中的示例产品属性正在被遗漏。

订单收集

[XmlRoot("Orders")]
public class OrderCollection : ICollection<Order>
{
    private static List<Order> _orders;

    private List<Order> Orders
    {
        get
        {
            if(_orders == null)
            {
                _orders = new List<Order>();
            }

            return _orders;
        }
    }

    //All the ICollection functions
}

顺序

public class Order
{
    [XmlElement("Id")]
    public int Id
    {
        get;
        set;
    }

    [XmlElement("Title")]
    public string Title
    {
        get;
        set;
    }

    [XmlElement("IsReserved")]
    public bool IsReserved
    {
        get;
        set;
    }

    [XmlElement("Count")]
    public int Count
    {
        get
        {
            return this.Products.Count;
        }
        set
        {

        }
    }

    // not serializing
    [XmlElement("Product")]
    public Product ProductTest
    {
        get
        {
            return new Product();
        }
    }

    // not serializing
    [XmlArray("Products")]
    public ICollection<Product> Products
    {
        get
        {
            return this._products;
        }

    }

    private List<Product> _products;

    public Order()
    {
        var rand = new Random();
        var count = rand.Next(1, 5);
        this._products = new List<Product>();

        for (int i = 0; i < count; i++)
        {
            this._products.Add(new Product());
        }
    }
}

产品

[Serializable]
public class Product 
{
    [XmlElement("Id")]
    public int Id
    {
        get;
        set;
    }

    [XmlElement("Title")]
    public string Title
    {
        get;
        set;
    }

    public Product()
    {
        var rand = new Random();
        this.Id = rand.Next(100, 9999);
        this.Title = "Product " + this.Id;
    }
}

输出

<?xml version="1.0"?>
<Orders xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Order>
    <Id>619883</Id>
    <Title>Order 619883</Title>
    <IsReserved>false</IsReserved>
    <Count>3</Count>
  </Order>
  <Order>
    <Id>961448</Id>
    <Title>Order 961448</Title>
    <IsReserved>false</IsReserved>
    <Count>3</Count>
  </Order>
  <Order>
    <Id>483677</Id>
    <Title>Order 483677</Title>
    <IsReserved>false</IsReserved>
    <Count>2</Count>
  </Order>
  <Order>
    <Id>512026</Id>
    <Title>Order 512026</Title>
    <IsReserved>false</IsReserved>
    <Count>2</Count>
  </Order>
  <Order>
    <Id>916029</Id>
    <Title>Order 916029</Title>
    <IsReserved>false</IsReserved>
    <Count>4</Count>
  </Order>
  <Order>
    <Id>109800</Id>
    <Title>Order 109800</Title>
    <IsReserved>false</IsReserved>
    <Count>4</Count>
  </Order>
</Orders>

我完全不知所措。深度序列化似乎正在起作用,因为否则订单不会在OrderCollection中被序列化,但它会停止。有什么建议吗?

3 个答案:

答案 0 :(得分:4)

您必须将ICollection更改为List。接口不可序列化。

[XmlArray("Products")]
public List<Product> Products
{
    get
    {
        return this._products;
    }

}

由于缺少二传手,产品测试不起作用

答案 1 :(得分:0)

您无法序列化界面..

界面只不过是对一组行为的描述。它没有说明实例的内容。特别是,虽然实现接口的类的实例必须实现其所有成员,但它肯定具有自己需要序列化的属性。

PS:从Serializing interfaces

复制

答案 2 :(得分:0)

更改为列表。

公共列表产品 {

}