繁体   English   中英

c#serialize嵌套类

[英]c# serialize nested class

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

订单收集

[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中被序列化,但它会停止。 有什么建议?

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

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

}

并且ProductTest因为缺少setter而无法工作

您无法序列化界面..

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

PS:从Serializing接口复制

转入列表。

公共列表产品{

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM