XmlSerializer.Deserialize()不反序列化一个List< T>正确地 [英] XmlSerializer.Deserialize() isn't deserializing a List<T> correctly

查看:1009
本文介绍了XmlSerializer.Deserialize()不反序列化一个List< T>正确地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的C#XmlSerializer的所以我可能会丢失一些基本的东西在这里。

I am new to the C# XmlSerializer so I might be missing something basic here.

我遇到的问题是,我有一个类,它有一个名单,其中,T> 另一个类的。当我序列化的主类的XML看起来漂亮,所有的数据是完整的。当我反序列化的XML,在数据的名单,其中,T> 消失,我留下一个空的名单,其中,T> 。我没有收到任何错误和序列化部分的工作方式类似的魅力。

The problem I am running into is that I have one class that has a List<T> of another class. When I serialize the main class the XML looks beautiful and all the data is intact. When I deserialize the XML, the data in the List<T> disappears and I am left with an empty List<T>. I am not receiving any errors and the serialization portion works like charm.

我缺少的是与反序列化进程?

What am I missing with the deserialization process?

编辑:请注意下图所示的code没有重现问题 - 它的工作原理。这是一个简化版本的真正code,是说的没有的工作。不幸的是,低于code的简化足够不重现此问题!

Note that the code shown below does not reproduce the problem - it works. This was a simplified version of the real code, which did not work. Unfortunately, the code below was simplified enough to not reproduce the problem!

public class User
{
  public User()
  {
    this.Characters = new List<Character>();
  }
  public string Username { get; set; }
  public List<Character> Characters { get; set; }
}

public class Character
{
  public Character()
  {
    this.Skills = new List<Skill>();
  }
  public string Name { get; set; }
  public List<Skill> Skills { get; set; }
}

public enum Skill
{
  TreeClimber,
  ForkliftOperator
}

public static void Save(User user)
{
    using (var textWriter = new StreamWriter("data.xml"))
    {
        var xmlSerializer = new XmlSerializer(typeof(User));
        xmlSerializer.Serialize(textWriter, user);
    }
}

public static User Restore()
{
    if (!File.Exists("data.xml"))
        throw new FileNotFoundException("data.xml");

    using (var textReader = new StreamReader("data.xml"))
    {
        var xmlSerializer = new XmlSerializer(typeof(User));
        return (User)xmlSerializer.Deserialize(textReader);
    }
}

public void CreateAndSave()
{
  var character = new Character();
  character.Name = "Tranzor Z";
  character.Skills.Add(Skill.TreeClimber);

  var user = new User();
  user.Username = "Somebody";
  user.Characters.Add(character);

  Save(user);
}

public void RestoreAndPrint()
{
  var user = Restore();
  Console.WriteLine("Username: {0}", user.Username);
  Console.WriteLine("Characters: {0}", user.Characters.Count);
}

执行CreateAndSave()生成的XML看起来像这样:

The XML generated by executing CreateAndSave() looks like so:

<User>
  <Username>Somebody</Username>
  <Characters>
    <Character>
      <Name>Tranzor Z</Name>
      <Skills>
        <Skill>TreeClimber</Skill>
      </Skills>
    </Character>
  <Characters>
</User>

完美!这是它的方式应该是。如果我再执行 RestoreAndPrint()我得到正确设置用户名属性用户对象,但人物属性是一个空列表:

Perfect! That's the way it should look. If I then execute RestoreAndPrint() I get a User object with the Username property set properly but the Characters property is an empty list:

Username: Somebody
Characters: 0

任何人都可以向我解释为什么字符属性是正确序列化,但不会反序列化?

Can anybody explain to me why the Characters property is serialized properly but won't deserialize?

推荐答案

与code很长一段时间我终于对使用XmlSerializer的默认行为的想法放弃了把玩左右后。

After futzing around with the code for a long time I finally gave up on the idea of using the default behaviour of the XmlSerializer.

所有的相关的类现在继承IXmlSerializer和实施的ReadXml()和中WriteXML()函数。我真的希望采取懒的路线,但现在,我已经打了IXmlSerializer我意识到,这真的是一点都不难,并增加了灵活性是非常好的。

All of the relevant classes now inherit IXmlSerializer and implement the ReadXml() and WriteXml() functions. I was really hoping to take the lazy route but now that I've played with IXmlSerializer I realize that it really isn't difficult at all and the added flexibility is really nice.

这篇关于XmlSerializer.Deserialize()不反序列化一个List&LT; T&GT;正确地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆