0

I'm trying to convert my xml file to a list of objects.

private void ReadChangelog()
    {
        XmlSerializer serializer = new XmlSerializer(typeof(Changelog));
        Changelog changelog = (Changelog)serializer.Deserialize(new StringReader("changelog.xml"));

        foreach (Release release in changelog.Releases)
        {
            string version = release.Version;
            string date = release.Date;
            string changes = release.Changes;
        }
    }

This is my changelog class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

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

    public Release[] Releases
    {
        get { return releases; }
        set { releases = value; }
    }
}

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

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

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

    [XmlAttribute]
    public string Changes
    {
        get { return changes; }
        set { changes = value; }
    }
}  

And this is my xml

<?xml version="1.0" encoding="utf-8" ?>
<Changelog>
  <Releases>
    <Release>
      <Version>1511</Version>
      <Date>25-11-2013</Date>
      <Changes>
        qzdqzdqzdqzdzdzzqefrsrgsrg
      </Changes>
    </Release>
    <Release>
      <Version>1510</Version>
      <Date>25-11-2013</Date>
      <Changes>
        Blabqzdzqdqzdqzd
      </Changes>
    </Release>
    <Release>
  </Releases>
</Changelog>

But when I try to run this I get an error

InvalidOperationException, The xml document (1,1) contains an error.

What am I doing wrong?

3
  • Did you try validating your XML w3schools.com/xml/xml_validator.asp ? Commented Nov 26, 2013 at 16:18
  • 1
    Does your last 'Release' need a closer? Commented Nov 26, 2013 at 16:19
  • You've used XmlAttribute to decorate the property, when the XML that you have is using "elements" to provide the Version, Date and Changes data. Either change your XML to put the data as "attributes" on the Release element, or change the way you define your Release class. Commented Nov 26, 2013 at 16:20

2 Answers 2

4

new StringReader("changelog.xml") doesn't read in the contents of changelog.xml. Instead, this code leaves you trying to deserialize the literal 'changelog.xml' string. I think you want something like new StreamReader("changelog.xml").

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

Comments

1

StringReader creates stream from string content, not from the file with given path. User StreamReader instead.

Changelog changelog = (Changelog)serializer.Deserialize(new StreamReader("changelog.xml"));

Despite of that, there are couple more errors in your code:

  1. Your document uses elements, and your class declaration uses XmlAttributeAttribute. It won't work together.
  2. Your document is not a correct XML document. It has some elements that are not closed.

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.