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