1

I'm trying to parse a xml file in an object

This is my xml file named Changelog.xml

<?xml version="1.0" encoding="utf-8" ?>
<Changelog>
  <Releases>
    <Release>
      <Version>1507</Version>
      <Date>22-11-2013</Date>
      <Changes>
        <Change>Change1</Change>
        <Change>Change2</Change>
        <Change>Change3</Change>
        <Change>Change4</Change>
      </Changes>
    </Release>
    <Release>
      <Version>1506</Version>
      <Date>20-11-2013</Date>
      <Changes>
        <Change>Change1</Change>
      </Changes>
    </Release>
  </Releases>
</Changelog>

This is my Changelog object I want to cast the xml to

[XmlRoot()]
public class Changelog
{
    private List<Release> releases;

    public List<Release> Releases
    {
        get { return releases; }
        set { releases = value; }
    }
}

public class Release
{
    private string version;
    private string date;
    private List<ChangeItem> changes;

    [XmlElement]
    public string Version
    {
        get { return version; }
        set { version = value; }
    }

    [XmlElement]
    public string Date
    {
        get { return date; }
        set { date = value; }
    }

    [XmlElement]
    public List<ChangeItem> Changes
    {
        get { return changes; }
        set { changes = value; }
    }
}

public class ChangeItem
{
    private string change;

    [XmlElement]
    public string Change
    {
        get { return change; }
        set { change = value; }
    }
}

Here I read the file

XmlSerializer serializer = new XmlSerializer(typeof(Changelog));
Changelog changelog = (Changelog)serializer.Deserialize(new StreamReader(@"changelog.xml"));
Releases = changelog.Releases;

foreach (Release release in Releases)
{
    string version = release.Version;
    string date = release.Date;
    List<ChangeItem> changes = release.Changes; // Has only 1 item
}

The problem is that there is only 1 object in the Changes list, altough I expect 4 for the 1507 release.

What am I doing wrong?

2 Answers 2

3

You have ChangeItem.Change property decorated with an XmlElement attribute. This means it becomes another element.

Thus the xml would need to look different.

To make it work, mark the property with the [XmlText] attribute.

Also, your Release.Changes needed to be decorated with a XmlArray and a XmlArrayItem attribute.

[XmlRoot]
public class Changelog
{
    private List releases;

    public List Releases
    {
        get { return releases; }
        set { releases = value; }
    }
}

public class Release
{
    private string version;
    private string date;
    private List changes;

    [XmlElement]
    public string Version
    {
        get { return version; }
        set { version = value; }
    }

    [XmlElement]
    public string Date
    {
        get { return date; }
        set { date = value; }
    }

    [XmlArray("Changes")]
    [XmlArrayItem("Change")]
    public List Changes
    {
        get { return changes; }
        set { changes = value; }
    }

}

public class ChangeItem
{
    private string change;

    [XmlText]
    public string Change
    {
        get { return change; }
        set { change = value; }
    }
}

I marked in bold what my changes were.

Sign up to request clarification or add additional context in comments.

Comments

0

change

[XmlElement]
public List<ChangeItem> Changes
{
    get { return changes; }
    set { changes = value; }
}

to

[XmlArray("Changes")]
public List<ChangeItem> Changes
{
    get { return changes; }
    set { changes = value; }
}

3 Comments

You forgot the XmlArrayItemAttribute and the XmlTextAttribute. See my answer for full code.
@SynerCoder That's true, missed that. You'll also need to be aware that you may find issues with the date format if you try to deserialize it on a machine with a different localisation setting. If possible the date should be in yyyy-MM-dd format
No problem, he serializes the data as a string. Issue circumvented.

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.