我在我的 xml 文件中反序列化列表列表时遇到问题:
<?xml version="1.0" encoding="UTF-8"?>
<RootLevel><!--Container-->
<ListOfOne><!--List of One -->
<One>
<ListOfTwo> <!--List of Two -->
<Two></Two>
</ListOfTwo>
</One>
</ListOfOne>
</RootLevel>
RootLevel 有一个列表。一个有两个列表
第一级(ListOfOne)是没有任何问题的工作文件,问题是 ListOfTwo 没有被反序列化
[KnownType(typeof(List<One>))]
[DataContract(Name = "RootLevel", Namespace = "")]
public sealed class RootLevel
{
[DataMember()]
public List<One> ListOfOne { get; set; }
public RootLevel()
{
ListOfOne = new List<One>();
}
}
[DataContract(Name = "One", Namespace = "")]
[KnownType(typeof(List<Two>))]
public sealed class One
{
public One()
{
ListOfTwo = new List<Two>();
}
[OnDeserialized]
internal void OnSerializingMethod(StreamingContext context)
{
ListOfTwo = new List<Two>();
}
[DataMember]
public List<Two> ListOfTwo { get; set; }
}
[DataContract(Name = "Two", Namespace = "")]
public sealed class Two
{}
这是操作:
using (var fs = new FileStream("path to file", FileMode.Open))
{
using (var reader = XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas()))
{
DataContractSerializer ser = new DataContractSerializer(typeof(RootLevel));
var deserializedPerson = (RootLevel)ser.ReadObject(reader, true);
Assert.IsTrue(deserializedPerson.ListOfOne[0].ListOfTwo.Count > 0);
reader.Close();
fs.Close();
}
}