XML反序列化问题:数组元素

13

我的 XML 是:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person>
  <id>sKQ0F4h1ft</id>
  <first-name>Govind</first-name>
  <last-name>Malviya</last-name>
  <positions total="3">
    <position>
      <id>sdfsdfsf</id>
      <title>Founder &amp; CEO</title>
      <summary>fsdsdf</summary>
      <start-date>
        <year>2010</year>
        <month>12</month>
      </start-date>
      <is-current>true</is-current>
      <company>
        <name>sdfsdf</name>
        <industry>Internet</industry>
      </company>
    </position>
    <position>
      <id>17908sdfsdf4226</id>
      <title>Engineer-in-traning</title>
      <summary></summary>
      <start-date>
        <year>2010</year>
        <month>3</month>
      </start-date>
      <is-current>true</is-current>
      <company>
        <id>sdfsdf</id>
        <name>sdfsdf</name>
        <industry>sfsdf sdfs sdf </industry>
      </company>
    </position>
    <position>
      <id>sdfsdff</id>
      <title>Graduate Researcher</title>
      <summary></summary>
      <start-date>
        <year>2006</year>
        <month>8</month>
      </start-date>
      <end-date>
        <year>2009</year>
        <month>1</month>
      </end-date>
      <is-current>false</is-current>
      <company>
        <id>sdfsdf</id>
        <name>University of Alberta</name>
        <type>Educational Institution</type>
        <industry>Higher Education</industry>
      </company>
    </position>
  </positions>
</person>

类是

[Serializable, XmlRoot("person")]
public class FooUserProfile
{
    [XmlElement("id")]
    public string ID { get; set; }

    [XmlElement("first-name")]
    public string FirstName { get; set; }

    [XmlElement("last-name")]
    public string LastName { get; set; }


    [XmlElement("positions")]
    public List<FooPosition> Positions { get; set; }
}

[Serializable]
public class FooPosition
{
    [XmlElement("id")]
    public string ID { get; set; }

    [XmlElement("title")]
    public string Title { get; set; }

    [XmlElement("summary")]
    public string Summary { get; set; }

    [XmlElement("start-date")]
    public FooDate StartDate { get; set; }

    [XmlElement("end-date")]
    public FooDate EndDate { get; set; }

    [XmlElement("is-current")]
    public string IsCurrent { get; set; }

    [XmlElement("company")]
    public FooPositionCompany Company { get; set; }
}

[Serializable]
public class FooDate
{
    [XmlElement("year")]
    public string Year { get; set; }

    [XmlElement("month")]
    public string Month { get; set; }
}

[Serializable]
public class FooPositionCompany
{
    [XmlElement("id")]
    public string ID { get; set; }

    [XmlElement("name")]
    public string Name { get; set; }

    [XmlElement("type")]
    public string Type { get; set; }

    [XmlElement("industry")]
    public string Industry { get; set; }
}

但是在某个位置却获取到了 null 值,请问有人能告诉我哪里出了问题吗?

2个回答

33

为了指定数组(IList, ICollection等)及其项的XML元素名称,您必须使用属性XmlArrayXmlArrayItem

[Serializable, XmlRoot("person")]
public class FooUserProfile
{
    /* The other members... */


    [XmlArray("positions")]
    [XmlArrayItem("position")]
    public List<FooPosition> Positions { get; set; }
}

XmlElement 属性的作用是忽略周围的 XML 数组元素,并将 Xml 数组项命名:

[XmlRoot("Config")]
public class Foo
{
  [XmlElement("Id")]
  public string[] IdStringArrayWithStupidName;
}

序列化的XML:

<?xml version="1.0" encoding="?>
<Config>
  <Id></Id>
  <Id></Id>
</Config>

你如何捕获列表标签中的“total =“ 3””或其他属性? - Matt N.
请查看XmlAttributeAttribute。 - Stefan
还是对这个语法不太熟悉,那我把它放在哪里?如果没有locations类,"total"是否只成为FooUserProfile中的一个公共字符串?我该如何告诉它我想要locations属性的属性? - Matt N.

0
对我来说,问题在于我使用了接口IList。
[XmlArray(elementName: "listItems", Order = 1)]
public IList<Item> ListItems { get; } = new List<Item>();

使用 List 而不是其他类型,所以反序列化失败了。

[XmlArray(elementName: "listItems", Order = 1)]
public List<Item> ListItems { get; } = new List<Item>();

编辑:这里有一个关于此问题的讨论使用C#将XML反序列化为IList


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接