使用XmlSerialize反序列化集合会导致空集合

时间:2012-01-11 21:40:38

标签: c# xml collections xml-deserialization

我正在尝试反序列化以下XML文档:

<?xml version="1.0" encoding="utf-8" ?>
<TestPrice>
  <Price>
    <A>A</A>
    <B>B</B>
    <C>C</C>     
    <Intervals>
      <Interval>
        <A>A</A>
        <B>B</B>
        <C>C</C>
      </Interval>
      <Interval>
        <A>A</A>
        <B>B</B>
        <C>C</C>
      </Interval>
    </Intervals>
  </Price>
</TestPrice>

我定义了三个类来将其反序列化为对象图:

public class TestPrice
{
    private List<Price> _prices = new List<Price>();
    public List<Price> Price 
    { 
        get { return _prices; }
        set { _prices = value; }
    }
}

public class Price
{
    public string A { get; set; }
    public string B { get; set; }
    public string C { get; set; }

    private List<Interval> _intervals = new List<Interval>();
    public List<Interval> Intervals
    {
        get { return _intervals; }
        set { _intervals = value; }
    }
}

public class Interval
{
    public string A { get; set; }
    public string B { get; set; }
    public string C { get; set; }
}

我可以将每个部分反序列化。也就是说,我可以做到:

var serializer = new XmlSerializer(typeof(Price));
var priceEntity = ((Price)(serializer.Deserialize(XmlReader.Create(stringReader))));

使用priceEntity中包含的XML数据正确初始化stringReader,包括List<Interval> Intervals。但是,如果我尝试反序列化TestPrice实例,则总会出现空List<Price> Price

如果我像这样改变TestPrice的定义:

public class TestPrice
{
    public Price Price { get; set; } 
}

有效。但当然我的XSD将Price定义为一个序列。我有其他实体反序列化很好,但它们不包括根元素中的序列。有没有我不知道的限制?我应该在TestPrice中包含某种元数据吗?

1 个答案:

答案 0 :(得分:3)

只需使用[XmlElement]装饰您的Price系列:

[XmlElement(ElementName = "Price")]
public List<Price> Price
{
    get { return _prices; }
    set { _prices = value; }
}

此外,您似乎正在反序列化Price,而XML中的根标记是TestPrice。所以,这是一个完整的例子:

public class TestPrice
{
    [XmlElement(ElementName = "Price")]
    public List<Price> Price { get; set; }
}

public class Price
{
    public string A { get; set; }
    public string B { get; set; }
    public string C { get; set; }

    public List<Interval> Intervals { get; set; }
}

public class Interval
{
    public string A { get; set; }
    public string B { get; set; }
    public string C { get; set; }
}

class Program
{
    static void Main()
    {
        var xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<TestPrice>
  <Price>
    <A>A</A>
    <B>B</B>
    <C>C</C>
    <Intervals>
      <Interval>
        <A>A</A>
        <B>B</B>
        <C>C</C>
      </Interval>
      <Interval>
        <A>A</A>
        <B>B</B>
        <C>C</C>
      </Interval>
    </Intervals>
  </Price>
</TestPrice>";

        var serializer = new XmlSerializer(typeof(TestPrice));
        using (var reader = new StringReader(xml))
        using (var xmlReader = XmlReader.Create(reader))
        { 
            var priceEntity = (TestPrice)serializer.Deserialize(xmlReader);
            foreach (var price in priceEntity.Price)
            {
                 // do something with the price
            }
        }
    }
}