I have XML that is returned by a 3rd party web service in the following form:
<object>
<list name="subscriptions">
<object>
<string name="id">something</string>
<string name="title">something else</string>
<list name="categories" />
<number name="timestamp">1252707770116</number>
</object>
...more 'object'...
</list>
</object>
I'm having a lot of issues trying to deserialize this data to an object. I wasn't able to generate a schema using xsd.exe, so I generated the following classes by hand to try and fit this data:
[XmlRoot("object")]
public class ListOfSubscriptions
{
[XmlElement("list")]
public Subscription[] items;
}
[XmlRoot("object")]
public class Subscription
{
[XmlAttribute()]
public string id;
[XmlAttribute()]
public string title;
[XmlAttribute()]
public string[] categories;
[XmlAttribute()]
public string timestamp;
}
I'm trying to deserialize this with the following code:
XmlSerializer s = new XmlSerializer(typeof(ListOfSubscriptions));
StreamReader r = new StreamReader("out.xml");
ListOfSubscriptions listSubscribe = (ListOfSubscriptions)s.Deserialize(r);
r.Close();
However, when it finishes, listSubscribe
has one member and all its fields are null.
How should I be creating my template for deserializing?
Thanks
Update - 2010/01/28
Thanks to everybody's comments I've revised my classes to the following:
[XmlRoot("object")]
public class ListOfSubscriptions
{
[XmlElement("list")]
public SubscriptionList[] items;
}
[XmlRoot("list")]
public class SubscriptionList
{
[XmlElement("object")]
public Subscription[] items;
}
[XmlRoot("object")]
public class Subscription
{
[XmlElement("string")]
public string id;
[XmlElement("string")]
public string title;
[XmlElement("list")]
public string[] categories;
[XmlElement("number")]
public string timestamp;
}
If I comment out the [XmlElement(...)] lines in Subscription, and run, I get that listSubscribe
has one SubscriptionList
item which has the correct number of Subscriptions, however, all the elements of the subscriptions are null.
If I uncomment the XmlElement lines, I get an error reflecting a Subscription. I imagine its getting confused because there are multiple elements with the same name.
How do I tie the attribute name to the class member?