【发布时间】:2013-12-16 17:27:02
【问题描述】:
我正在尝试将 Xml 文档反序列化为对象数组。我用 Xna 做到了,但对于单人游戏,我必须改变我的方法。
这就是我反序列化的方式:
public static XmlData[] DeserializeFromXml<XmlData>(string inputFile)
{
XmlSerializer s = new XmlSerializer(typeof(XmlData));
XmlData[] deserializedObject = default(XmlData[]);
byte[] byteArray = Encoding.UTF8.GetBytes(inputFile);
//byte[] byteArray = Encoding.ASCII.GetBytes(contents);
MemoryStream stream = new MemoryStream(byteArray);
using (TextReader textReader = new StreamReader(stream))
{
deserializedObject = (XmlData[])s.Deserialize(textReader);
}
return deserializedObject;
}
我的班级 XmlData:
public class XmlData
{
public int id;
public int posx;
public int posy;
public int rot;
public int Width;
public int Height;
}
我的 xml 文件:
<?xml version="1.0" encoding="utf-8" ?>
<Asset Type="XmlData[]">
<Item>
<id>17</id>
<posx>54</posx>
<posy>30</posy>
<rot>90</rot>
<Width>184</Width>
<Height>5</Height>
</Item>
<Item>
<id>1</id>
<posx>200</posx>
<posy>120</posy>
<rot>0</rot>
<Width>100</Width>
<Height>90</Height>
</Item>
</Asset>
我有以下错误:
There is an error in XML document (1, 1). (i'm using monogame)
【问题讨论】:
-
你的根的结束标签有一个首字母小写。
-
开始标签是
Asset(带有大写A)和结束标签asset是问题中的错字,还是您的XML内容是这样的? -
无误点击,都是大写
-
尝试返回 List
和这个 XmlSerializer s = new XmlSerializer(typeof(List )); -
@Gabson:看来你在Can't Load asset as XML file 之前问过同样的问题??
标签: c# xml deserialization