C#DataContractSerializer - 反序列化列表列表

时间:2013-09-12 11:18:56

标签: c# xml list datacontractserializer

我在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();
                }
            }

1 个答案:

答案 0 :(得分:1)

如果删除这部分代码,一切都按预期工作:

[OnDeserialized]
internal void OnSerializingMethod(StreamingContext context)
    ListOfTwo = new List<Two>();
}

如果您希望确保总是有空的ListOfTwo,请将其更改为:

[OnDeserialized]
internal void OnSerializingMethod(StreamingContext context)
{
    if(ListOfTwo == null) {
        ListOfTwo = new List<Two>();
    }
}

我通过一个小修改(不是从文件中读取)来运行代码

string 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>";

var stream = new MemoryStream(Encoding.Default.GetBytes(xml));

using (var reader = XmlDictionaryReader
                           .CreateTextReader(stream, 
                                             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();
}

此合同变更

[DataContract(Name = "One", Namespace = "")]
[KnownType(typeof(List<Two>))]
public sealed class One
{
    public One()
    {
        ListOfTwo = new List<Two>();
    }

    [OnDeserialized]
    internal void OnSerializingMethod(StreamingContext context)
    {
        if (ListOfTwo == null)
        {
            ListOfTwo = new List<Two>();
        }
    }

    [DataMember]
    public List<Two> ListOfTwo { get; set; }
}

Assert很好,ListOfTwo有一个预期的对象。