XML文档中的错误

时间:2013-12-13 14:44:39

标签: c# xml deserialization

我正在尝试将Xml文档反序列化为对象数组。我是用Xna做的,但是对于monogame,我必须改变我的方法。

这是我反序列化的方式:

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)

4 个答案:

答案 0 :(得分:1)

Encoding.UTF8.GetBytes(inputFile);

您正在尝试解码文件名,而不是文件内容。

尝试类似

的内容
using (StreamReader reader = StreamReader(inputFile,Encoding.UTF8,true)) 
{
    XmlSerializer serializer = new XmlSerializer(typeof(XmlData));
    return (XmlData[])serializer.Deserialize(reader);
}

答案 1 :(得分:1)

确定这将适用于您的xml文件:

public static List<XmlData> DeserializeFromXml(string inputFile)
{
     List<XmlData> mydata = new List<XmlData>();
     XmlSerializer s = new XmlSerializer(typeof(List<XmlData>),new XmlRootAttribute("Asset"));
     //XmlData[] deserializedObject = default(XmlData[]);
     //byte[] byteArray = Encoding.UTF8.GetBytes(inputFile);
     //byte[] byteArray = Encoding.ASCII.GetBytes(contents);
     //MemoryStream stream = new MemoryStream(byteArray);
     using (TextReader txtReader = new StreamReader(inputFile))
     {
        mydata = (List<XmlData>)s.Deserialize(txtReader);
     }

     return mydata;
}

<Item>更改为<XmlData>并且您很好,或者将其放在您的XmlData类声明中:

[XmlType("Item")]
public class XmlData

答案 2 :(得分:0)

你可以使用这个代码!!

       using (XmlTextReader xmlReader = new XmlTextReader(yourfile))
        {
            XDocument xdoc = XDocument.Load(xmlReader);
            var programs= from programItem in xdoc.Root.Elements()
                          select new xmldata {
                Id  = Convert.ToInt32( programItem.Attribute("Id").Value),
                posx  = Convert.ToInt32(   programItem.Attribute("posx").Value),
                poxy = Convert.ToInt32( programItem.Attribute("poxy").Value),
            };

            result = programs.ToList();
        }

答案 3 :(得分:0)

感谢@terrybozzio,我终于找到了一种方法来读取我的xml文件,然后才将其转换为流。但是使用 Monogame Framework 并没有实现alll方法。

方法:

  public static List<XmlData> DeserializeFromXml(string inputFile)
    {
        List<XmlData> mydata = new List<XmlData>();
        XmlSerializer s = new XmlSerializer(typeof(List<XmlData>), new XmlRootAttribute("Asset"));
        //XmlData[] deserializedObject = default(XmlData[]);
       // byte[] byteArray = Encoding.UTF8.GetBytes(inputFile);
     //   byte[] byteArray = Encoding.ASCII.GetBytes(inputfile);
       // MemoryStream stream = new MemoryStream(byteArray);

        Stream test = TitleContainer.OpenStream("pets.xml");
        using (TextReader txtReader = new StreamReader(test))
        {
            mydata = (List<XmlData>)s.Deserialize(txtReader);
        }

        return mydata;
    }