使用XmlSerializer忽略外部元素

时间:2013-07-07 16:28:31

标签: c# xml xml-serialization

我正在尝试反序列化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.

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

2 个答案:

答案 0 :(得分: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; }
    }
    

答案 1 :(得分: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);
        }