SameOldNick
SameOldNick

Reputation: 2437

Unable to deserialize XML to List<>

I'm having problems trying to get the following XML code to work. First the exception was saying <items xmlns=''> was not expected. and I seem to have been able to fix that by specifying XmlRootAttribute. Now though, it comes back with an empty List<Items> and I can't figure out why. Any ideas?

XML Example

<?xml version="1.0"?>
<items>
    <item>
        <version>1.0</version>
        <title>Example</title>
        <changelog>http://example.com/changelog.txt</changelog>
        <url>http://www.example.com/download/</url>
    </item>
</items>

XML Deserialize Code

Stream appCastStream = webResponse.GetResponseStream();
UpdateXML updateXml = new UpdateXML();

var rootAttribute = new XmlRootAttribute();
rootAttribute.ElementName = "items";
rootAttribute.IsNullable = true;

XmlSerializer serializer = new XmlSerializer(typeof(UpdateXML), rootAttribute);

try
{
    using (XmlTextReader reader = new XmlTextReader(appCastStream))
    {
        if (serializer.CanDeserialize(reader))
        {
            updateXml = (UpdateXML)serializer.Deserialize(reader);
        }
        else
        {
            throw new Exception("Update file is in the wrong format.");
        }
    }
}
catch (Exception ex)
{
    Debug.WriteLine("The following error occurred trying to check for updates: {0}", new object[] { ex.Message });

    return;
}

UpdateXML Code

public class UpdateXML
{
    public class Item
    {
        private string _versionString;
        private string _title;
        private string _changelog;
        private string _url;

        [XmlElement("version")]
        public string VersionString
        {
            get { return this._versionString; }
            set { this._versionString = value; }
        }

        public Version Version
        {
            get 
            {
                if (string.IsNullOrEmpty(this._versionString))
                    return null;

                return new Version(this._versionString); 
            }
        }

        [XmlElement("title")]
        public string Title
        {
            get { return this._title; }
            set { this._title = value; }
        }

        [XmlElement("changelog")]
        public string ChangeLog
        {
            get { return this._changelog; }
            set { this._changelog = value; }
        }

        [XmlElement("url")]
        public string URL
        {
            get { return this._url; }
            set { this._url = value; }
        }
    }

    private List<Item> _items = new List<Item>();

    [XmlArray("items")]
    [XmlArrayItem("item")]
    public List<Item> Items
    {
        get { return this._items; }
        set { this._items = value; }
    }

    public UpdateXML()
    {

    }
}

Upvotes: 0

Views: 355

Answers (1)

Andrew Whitaker
Andrew Whitaker

Reputation: 126042

I think the problem is that your XML doesn't really have a "root" element-- the top level element is an array. This answer applied to your problem seems to fix things (also removing the rootAttribute argument passed to the XmlSerializer):

[XmlRootAttribute("items")]
public class UpdateXML
{
    private List<Item> _items;

    [XmlElement("item")]
    public List<Item> Items
    {
        get { return this._items; }
        set { this._items = value; }
    }
}

Example: https://dotnetfiddle.net/MAet5y

If you're able to modify the XML, you could add a root element and that would fix the issue as well.

For example, if you wrapped your current XML in <root></root> and then modified your UpdateXML class as follows:

[XmlRootAttribute("root")]
public class UpdateXML
{    
    private List<Item> _items;

    [XmlArray("items")]
    [XmlArrayItem("item")]
    public List<Item> Items
    {
        get { return this._items; }
        set { this._items = value; }
    }
}

Example: https://dotnetfiddle.net/6KfxA4

Upvotes: 1

Related Questions