我在将XML反序列化到位置列表对象时遇到问题 给出以下XML:
<?xml version="1.0" encoding="utf-8"?>
<locations>
<location id="1">
<level name="3" complete="True" stars="1" firstMisson="True" secondMission="False" thridMission="False" />
</location>
<location id="2">
<level name="4" complete="True" stars="3" firstMisson="True" secondMission="True" thridMission="True" />
</location>
</locations>
以下课程:
[System.Serializable]
[XmlRoot(ElementName = "level")]
public class Level
{
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "complete")]
public string Complete { get; set; }
[XmlAttribute(AttributeName = "stars")]
public string Stars { get; set; }
[XmlAttribute(AttributeName = "firstMisson")]
public string FirstMisson { get; set; }
[XmlAttribute(AttributeName = "secondMission")]
public string SecondMission { get; set; }
[XmlAttribute(AttributeName = "thridMission")]
public string ThridMission { get; set; }
}
[System.Serializable]
[XmlRoot(ElementName = "location")]
public class Location
{
[XmlElement(ElementName = "level")]
public Level Level { get; set; }
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
}
[System.Serializable]
[XmlRoot(ElementName = "locations")]
public class Locations
{
[XmlElement(ElementName = "location")]
public Location Location { get; set; }
public List<Locations> LocDb = new List<Locations>();
}
[System.Serializable]
[XmlRoot(ElementName = "xml")]
public class Xml
{
[XmlElement(ElementName = "locations")]
public Locations Locations { get; set; }
}
反序列化方法
public List<Locations> locDB = new List<Locations>();
public static void LoadData()
{
string filepath = Application.dataPath + @"/XML/GameXMLdata.xml";
var xmlSerializer = new XmlSerializer(locDB.GetType());
var stream = File.Open(filepath, FileMode.Open);
locDB = (List<Locations>)xmlSerializer.Deserialize(stream);//locations xmlns=''> was not expected
stream.Close();
Debug.Log(locDB[1].Location.Id);
}
那么如何将XML反序列化为位置对象列表?
非常感谢您的帮助。
答案 0 :(得分:1)
您只需要两个课程:
[XmlType("level")]
public class Level
{
[XmlAttribute("name")]
public string Name { get; set; }
[XmlAttribute("complete")]
public string Complete { get; set; }
[XmlAttribute("stars")]
public string Stars { get; set; }
[XmlAttribute("firstMisson")]
public string FirstMisson { get; set; }
[XmlAttribute("secondMission")]
public string SecondMission { get; set; }
[XmlAttribute("thridMission")]
public string ThridMission { get; set; }
}
[XmlType("location")]
public class Location
{
[XmlElement("level")]
public Level Level { get; set; }
[XmlAttribute("id")]
public string Id { get; set; }
}
重要的是将XmlType
属性应用于课程Location
而不是XmlRoot
。
现在,您可以将XML反序列化为List<Location>
,如下所示:
var xmlSerializer = new XmlSerializer(typeof(List<Location>),
new XmlRootAttribute("locations"));
List<Location> locations;
using (var stream = File.OpenRead(filepath))
locations = (List<Location>)xmlSerializer.Deserialize(stream);
诀窍是使用XmlSerializer(Type, XmlRootAttribute)
构造函数重载指定根元素名称(在您的情况下为“locations”)。
答案 1 :(得分:0)
我在类属性上遇到了类似的问题,并使用不同的方法解决了它。由于在我的场景中我无法触及 XmlSerializer
构造逻辑,这就是我所做的(使用来自 OP 的类型作为参考):
[XmlRoot("locations")]
public sealed class LocationCollection : Collection<Location>
{
}
使用自定义集合类,我可以在设计时设置根目录并仍然使用标准的 XmlSeriaizer
实例。