3

我正在尝试反序列化 XML 文档:

<?xml version='1.0' encoding='UTF-8'?>
<eveapi version="2">
  <currentTime>2013-07-07 07:24:20</currentTime>
  <result>
    <rowset name="characters" key="characterID" columns="name,characterID,corporationName,corporationID">
      <row name="xxxxx" characterID="1234" corporationName="xxxx" corporationID="1234" />
    </rowset>
  </result>
  <cachedUntil>2013-07-07 07:40:39</cachedUntil>
</eveapi>

我的模型是:

[XmlRoot("rowset")]
public class CharacterList
{
    public CharacterList() { Characters = new List<Character>(); }

    [XmlElement("row")]
    public List<Character> Characters { get; set; }
}

public class Character
{
    [XmlElement("name")]
    private string name { get; set; }

    [XmlElement("characterID")]
    private int Id { get; set; }

    [XmlElement("corporationName")]
    private string corporationName { get; set; }

    [XmlElement("corporationID")]
    private int corporationId { get; set; }
}

我的反序列化代码是:

XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "result";
xRoot.IsNullable = true;
var serializer = new XmlSerializer(typeof(Character), xRoot);
var list = (CharacterList) serializer.Deserialize(output);

但是,我遇到了一个例外:

System.InvalidOperationException: There is an error in XML document (2,2).

具有内部类型:

System.InvalidOperationException: <eveapi xmlns=''> was not expected.

我很确定这是因为我不需要外部信息。有没有办法可以忽略它?我的另一个想法是我可以为模式的其余部分编写包装类,然后忽略我不关心的内容。但是,我希望有一个更简单的方法。我已经坚持了一段时间,任何帮助将不胜感激。

4

2 回答 2

2

您可以使用 XmlReader 导航到内部元素并从那里使用 XmlSerializer:

   using (XmlReader reader = XmlReader.Create("c:\\your.xml"))
    {
        reader.MoveToContent();
        reader.ReadToDescendant("rowset");
        var serializer = new XmlSerializer(typeof(CharacterList));
        var list = (CharacterList)serializer.Deserialize(reader);
    }

另请注意,您的模型中也存在一些问题:

  • 属性应该是公开的。
  • 对属性使用 XmlAttribute 而不是 XmlElement
  • 使用 typeof(CharacterList) 而不是 typeof(Character)

    [XmlRoot("rowset")]
    public class CharacterList
    {
        public CharacterList() { Characters = new List<Character>(); }
    
        [XmlElement("row")]
        public List<Character> Characters { get; set; }
    }
    
    public class Character
    {
        [XmlAttribute("name")]
        public  string name { get; set; }
    
        [XmlAttribute("characterID")]
        public int Id { get; set; }
    
        [XmlAttribute("corporationName")]
        public string corporationName { get; set; }
    
        [XmlAttribute("corporationID")]
        public int corporationId { get; set; }
    }
    
于 2013-07-07T17:11:49.073 回答
1

我不习惯 XMLSerialzer,但是改用 Linq-to-XML 怎么样?

        string pathToXML = "";
        XDocument doc = XDocument.Load(pathToXML);

        var qry = from ele in doc.Descendants("row")
                     select new
                     {
                         name = ele.Attribute("name").Value,
                         charID = Convert.ToInt32(ele.Attribute("characterID").Value),
                         corName = ele.Attribute("corporationName").Value,
                         corID =  Convert.ToInt32(ele.Attribute("corporationID").Value)
                     };

        foreach (var element in qry)
        {
            Console.WriteLine(element.name + " " + element.charID + " " + element.corName + " " + element.corID);
        }
于 2013-07-07T17:10:04.290 回答