将自定义对象列表序列化/反序列化到 xml 时出错

本文关键字:xml 出错 反序列化 自定义 对象 列表 序列化 | 更新日期: 2023-09-27 18:33:14

我有这个类:

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 时,我得到一个异常,指出根元素丢失。这是怎么回事?

将自定义对象列表序列化/反序列化到 xml 时出错

此反序列化代码应有效:

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