无法从xml文件xamarin表单反序列化对象

时间:2016-12-13 00:01:20

标签: android xml serialization xamarin xamarin.forms

所以我试图在我的xamarin表单项目中序列化和反序列化一个名为player的对象。 这就是玩家的样子:

    public class Player 
    {

        //stores weather the player ended his turn
        public bool turnOver = false;
        //the name of the player

        public string name { get; set; }
        //total score of the player

        public long score { get; set; }
        //coins to buy abillities

        public int coins { get; set; }
        //array that stores for each ability how much  uses left

        public int[] abilities = { 2, 2, 2, 2 };
        //the levels the player have completed

        public List<long> completedLevels;
        //player constructor that initializes all the data for initial use
        public Player()
        {
            this.name = "";
            score = 0;
            coins = 100;
            completedLevels = new List<long>();
        }
      }

我在Android项目中使用这些方法来序列化和反序列化对象。

    public void Serialize<T>(Player list)
    {
            //Creating XmlSerializer.
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            var filePath = Path.Combine(documentsPath, "data1.xml");
            var file = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.Write);
            var strm = new StreamWriter(file);
            //Convert the XML to List
            serializer.Serialize(strm, list);
            strm.Close();
    }

    public T GenericDeSerialize<T>()
    {
        //Creating XmlSerializer for the object
        XmlSerializer serializer = new XmlSerializer(typeof(T));
        var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        var filePath = Path.Combine(documentsPath, "data1.xml");
        var file = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.Read);
        var strm = new StreamReader(file);
        string text = strm.ReadToEnd();
        //Deserialize back to object from XML
        T b = (T)serializer.Deserialize(strm);
        strm.Close();
        return b;

    }

现在序列化部分运行良好,但在尝试反序列化时,我得到了异常:

  

缺少根元素

我看一下生成的xml,它看起来像那样:

    <?xml version="1.0" encoding="utf-8"?>
<Player xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <turnOver>false</turnOver>
  <abilities>
    <int>2</int>
    <int>2</int>
    <int>2</int>
    <int>2</int>
  </abilities>
  <completedLevels />
  <name />
  <score>0</score>
  <coins>500</coins>
</Player>

我无法找到问题,任何人都可以指出为什么xmlserializer可能会写一些东西并且无法读取它? 感谢

编辑: 以下是我现在调用它们进行测试的方法。序列化程序是具有这两个函数的类。

        Serializer ser = new Serializer();
        Player p = new Player();
        p.coins = 500;
        ser.Serialize<Player>(p);
       ser.GenericDeSerialize<Player>();

1 个答案:

答案 0 :(得分:1)

在序列化过程中,代码执行此操作:

  XmlSerializer serializer = new XmlSerializer(typeof(T));

但是在反序列化期间,它会这样做:

  XmlSerializer serializer = new XmlSerializer(typeof(List<T>));

您正在尝试反序列化List&lt;播放器&gt;,但你序列化了播放器,你的XML只显示一个播放器,而不是列表&lt;播放器&gt;。您需要序列化和反序列化相同的类型。

修改

第二个问题是在反序列化期间,代码试图两次使用流:

    string text = strm.ReadToEnd();
    //Deserialize back to object from XML
    T b = (T)serializer.Deserialize(strm);

strm.ReadToEnd()调用将使用流,不为serializer.Deserialize调用留下任何内容。要么去除strm.ReadToEnd()调用(代码不使用'text'),要从'text'反序列化,要么将流重置为调用之间的开头。