13

My XML is

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

Classes are

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

but in position I am getting nulls can anybody tell me where I am wrong.

2 Answers 2

34

In order to specify the XML element names of an array (IList, ICollection, etc.) and it's items, you have to use the attributes XmlArray and XmlArrayItem:

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


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

The attribute XmlElement has the effect that the surrounding XML array element will be omitted and give the Xml array items it's names:

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

Serialized XML:

<?xml version="1.0" encoding="?>
<Config>
  <Id></Id>
  <Id></Id>
</Config>
Sign up to request clarification or add additional context in comments.

3 Comments

how would you capture the 'total="3"'? Or other attributes in the list tag?
Have a look for the XmlAttributeAttribute.
Still new to this syntax, so where do I put this? If there isn't a locations class does "total" just become a public string in FooUserProfile? How to I tell it that I want the attributeattribute of locations?
0

For me the issue was that I was using the interface IList

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

instead of just List, so the deserialization did not work.

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

Edit: There is a thread about this here Deserialize xml to IList c#

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.