【发布时间】:2012-02-08 06:27:09
【问题描述】:
我正在尝试反序列化以下 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 使用包含在stringReader 中的XML 数据正确初始化,包括List<Interval> Intervals。但是,如果我尝试反序列化 TestPrice 实例,它总是会出现一个空的 List<Price> Price。
如果我像这样更改TestPrice 的定义:
public class TestPrice
{
public Price Price { get; set; }
}
它有效。但当然,我的 XSD 将 Price 定义为一个序列。我有其他实体反序列化很好,但它们不包括根元素中的序列。有我不知道的限制吗?我应该在TestPrice 中包含某种元数据吗?
【问题讨论】:
标签: c# xml collections xml-deserialization