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()
{
}
}