This questions was asked before but the answer doesn't work for me: so here is the question again : )
I am having real trouble trying to deserialize some XML and was hoping someone can offer some assistance. I have read a lot of similar posts but I am unable to resolve this.
Here is the XML
<register-account success="false">
<user-name>xxxxx</user-name>
<password>fghgh</password>
<email>test@example.com</email>
</register-account>
Class I am trying to deserialize to:
[Serializable, XmlRoot(ElementName = "register-account", Namespace = "MyNamespace")]
[XmlType("register-account")]
public class RegisterAccountResponse
{
[XmlAttribute("success")]
public bool Success { get; set; }
/// <summary>
/// Gets or sets the Tennant email address
/// </summary>
[XmlElement("email")]
public string Email { get; set; }
/// <summary>
/// Gets or sets the tennant password
/// </summary>
[XmlElement("password")]
public string Password { get; set; }
/// <summary>
/// Gets or sets the Tennant username
/// </summary>
[XmlElement("user-name")]
public string Username { get; set; }
}
Deserialization Method
public static T Deserialize<T>(string data) where T : class
{
var ser = new XmlSerializer(typeof(T));
using (var sr = new StringReader(data))
{
return (T)ser.Deserialize(sr);
}
}
Deserialization Method Call
var data = Helper.Deserialize<RegisterAccountResponse>(xml);
Exception:
There is an error in XML document (1, 2). ---> System.InvalidOperationException: was not expected. at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderData.Read5_data()
Inner Exception as follows:
<register-account xmlns=''> was not expected.
The answer to the question was to remove the namespace from the class (remove Namespace = "MyNamespace"). The class however is part of a very large library that is autogenerated using xsd.exe. I don't want to change anything in the generated class. Is there away to still fix this issue without having to modify the class?