我从这个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; }
}
}
不知何故,它只占用图像中的第一条路径而不记录其余路径。我做错了吗?
答案 0 :(得分: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>
答案 1 :(得分: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
{
}