错误序列化/反序列化自定义对象列表到/从xml

时间:2016-02-03 18:58:40

标签: c# xml-serialization

我有这堂课:

using System;
using System.Xml.Serialization;

namespace RPG_Maker_WPF.Models
{
    /// <summary>
    /// Represents a (theoretically) playable character.
    /// </summary>
    public class Actor
    {
        #region Properties

        /// <summary>
        /// The name of this actor.
        /// </summary>
        public string Name
        {
            get { return _name; }
          set { _name = value; }
        }
        private string _name;

        /// <summary>
        /// The second name of this actor.
        /// </summary>
        public string SecondName
        {
            get { return _secondName; }
            set { _secondName = value; }
        }
        private string _secondName;

        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }
        private string _description;

        /// <summary>
        /// The initial level of this actor.
        /// </summary>
        public int InitialLevel
        {
            get { return _initialLevel; }
            set { _initialLevel = value; }
        }
        private int _initialLevel;

        /// <summary>
        /// The maximum level of this actor.
        /// </summary>
        public int MaxLevel
        {
            get { return _maxLevel; }
            set
            { _maxLevel = value; }
        }
        private int _maxLevel;

        #endregion Properties

        public Actor()
        {

        }
    }
}

在我的另一堂课中,我有List<Actor>。现在我想将此列表反序列化为xml。我使用以下内容:

[XmlRoot(ElementName = "Database")]
    public class Database
    {
        #region Properties

        [XmlArray("Actors"), XmlArrayItem(typeof(Actor), ElementName = "Actor")]
        public List<Actor> Actors
        {
            get { return _actors; }
            private set { _actors = value; }
        }
        private List<Actor> _actors;

        #endregion properties

        public Database()
        {
            Actors = new List<Actor>();
        }

        public void LoadData()
        {
            Actors.Add(new Actor() { Name = "Test", SecondName = "Testi", InitialLevel = 1, MaxLevel = 44, Description = "Bla" });
            SaveData();

            XmlSerializer serializer = new XmlSerializer(typeof(List<Actor>));
            FileStream fs = new FileStream(ProjectViewModel.CurrentProject.Path + "\\Data\\ActorData.rpgwpfd", FileMode.Open);
            XmlReader reader = XmlReader.Create(fs);

            Actors = (List<Actor>)serializer.Deserialize(fs);
        }

        public void SaveData()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<Actor>));
            string path = ProjectViewModel.CurrentProject.Path + "\\Data\\ActorData.rpgwpfd";
            FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
            serializer.Serialize(fs, Actors);
            fs.Close();
        }
    }
}

我从中得到的xml看起来像这样:

<?xml version="1.0"?>
<ArrayOfActor xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Actor>
    <Name>Test</Name>
    <SecondName>Testi</SecondName>
    <Description>Bla</Description>
    <InitialLevel>1</InitialLevel>
    <MaxLevel>44</MaxLevel>
  </Actor>
</ArrayOfActor>

但是在尝试反序列化xml时,我得到一个异常,说根元素丢失了。这有什么不对?

1 个答案:

答案 0 :(得分:0)

这个反序列化代码应该有效:

        using (var fs = File.OpenRead(ProjectViewModel.CurrentProject.Path + "\\Data\\ActorData.rpgwpfd"))
        {
            var serializer = new XmlSerializer(typeof(List<Actor>));
            Actors = (List<Actor>)serializer.Deserialize(fs);
        }