Trying to deserialize List. Im getting the following error:
System.InvalidOperationException: There is an error in XML document (1, 2). ---> System.InvalidOperationException: was not expected.
Saw other quesitons like : {"<user xmlns=''> was not expected.} Deserializing Twitter XML but this does not solve my problem either.
This is an Xml Sample
<authorizations>
<id>111</id>
<name>Name 1</name>
<Lists>
<List>
<id>1</id>
<id>2</id>
</List>
</Lists>
</authorizations>
<authorizations>
<id>222</id>
<name>Name 2</name>
<List />
</authorizations>
<authorizations>
<id>333</id>
<name>Name 3</name>
<List />
</authorizations>
The class are created as follow:
public class Authorization
{
[XmlElement("id")]
public string Id{ get; set; }
[XmlElement("name")]
public string Name{ get; set; }
[XmlArray("Lists")]
[XmlArrayItem("List")]
public List[] Items{ get; set; }
}
public class List
{
[XmlElement("id")]
public string Id{ get; set; }
}
public class AuthorizationList
{
[XmlArray("authorizations")]
public Authorization Authorizations{ get; set; }
}
Have tried changing the list to XmlArray, XmlArrayItem or Element. but still get the same error when I deserialize.
Deserializing Code Sample:
public static T FromXml<T>(string xmlString)
{
T obj = default(T);
if (!string.IsNullOrWhiteSpace(xmlString))
{
using (var stringReader = new StringReader(xmlString))
{
var xmlSerializer = new XmlSerializer(typeof(T));
obj = (T)xmlSerializer.Deserialize(stringReader);
}
}
return obj;
}