使用XmlSerializer忽略外部元素 [英] Ignore outer elements using XmlSerializer

查看:40
本文介绍了使用XmlSerializer忽略外部元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to deserialize the XML document:

<?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);

但是,我遇到一个例外:

However, I am getting an exception:

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

具有内部类型:

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

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

I'm pretty sure this is because of the outer information I do not need. Is there a way I can ignore it? my other thought was I could write wrapper classes for the rest of the schema, then just ignore what I don't care about. However, I was hoping there is an easier way. I've been stuck on this for a while, any help would be appreciated.

推荐答案

您可以使用XmlReader导航到内部元素,然后从此处使用XmlSerializer:

You can use an XmlReader to navigate to the inner element and use the XmlSerializer from there:

   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);
    }

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

Please also note that there are also some issues in your model:

  • 财产应该是公开的.
  • 使用XmlAttribute代替XmlElement作为属性
  • 使用typeof(CharacterList)代替typeof(Character)

  • Properties should be public.
  • Use XmlAttribute for attributes instead of XmlElement
  • Use typeof(CharacterList) instead of 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; }
}

这篇关于使用XmlSerializer忽略外部元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆