2

我正在从此 xml 文件加载不同类型的路径:

<?xml version="1.0" encoding="utf-8" ?>
<SplashScreen>
  <Image>
    <Path>Visual/Screens/SplashScreen/samurai1024-768</Path>
    <Path>Visual/Screens/SplashScreen/Humanoid1024-768</Path>
  </Image>
  <Song>
    <Path>Audio/SplashScreen/wardrums</Path>
  </Song>
</SplashScreen>

这是我用来序列化和反序列化的 XmlManager 类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace Lucid.Classes
{
    public class XmlManager<T>
    {
        public Type Type;

        public T Load(string P_path)
        {
            T instance;
            using (TextReader reader = new StreamReader(P_path))
            {
                XmlSerializer xmlSerializer = new XmlSerializer(Type);
                instance = (T)xmlSerializer.Deserialize(reader);
            }
            return instance;
        }

        public void Save(string P_path, object P_obj)
        {
            using (TextWriter writer = new StreamWriter(P_path))
            {
                XmlSerializer xmlSerializer = new XmlSerializer(Type);
                xmlSerializer.Serialize(writer, P_obj);
            }
        }
    }
}

这是我要反序列化的类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace Lucid.Classes
{
    [XmlRoot("SplashScreen")]
    public class SerializableSplashScreen
    {
        [XmlElement("Image")]
        public List<string> imgPathList { get; set; }
        [XmlElement("Song")]
        public List<string> songPathList { get; set; }
    }
}

不知何故,它只接受图像中的第一个路径,而不记录其余部分。难道我做错了什么?

4

2 回答 2

1

好的,我找到了解决方案。问题出在 xml 文件中。正确的写法是:

<?xml version="1.0" encoding="utf-8" ?>
<SplashScreen>
  <Image>Visual/Screens/SplashScreen/samurai1024-768</Image>
  <Image>Visual/Screens/SplashScreen/Humanoid1024-768</Image>
  <Song>Audio/SplashScreen/wardrums</Song>
</SplashScreen>
于 2013-09-24T03:24:36.210 回答
0

试试这个类定义,你应该能够正确地反序列化你的 XML:

[Serializable]
[XmlRoot("SplashScreen")]
public class SerializableSplashScreen
{
    [XmlElement]
    public Image Image { get; set; }

    [XmlElement]
    public Song Song { get; set; }
}

[Serializable]
public class IsPath
{
    [XmlElement]
    public List<string> Path { get; set; }
}

[Serializable]
public class Image : IsPath
{
}

[Serializable]
public class Song : IsPath
{
}
于 2013-09-24T03:39:00.010 回答