Error Deserializing Xml to Object - xmlns='' was not expected Error Deserializing Xml to Object - xmlns='' was not expected xml xml

Error Deserializing Xml to Object - xmlns='' was not expected


Simply take off the Namespace =:

[XmlRoot("register-account"), XmlType("register-account")]public class RegisterAccountResponse {...}

since your xml doesn't seem to be in an xml-namespace. Also, [Serializable] isn't used by XmlSerializer.

If your xml was using a namespace it would have an xmlns at the root.

Also, to help with callers you could add where T : class, new() (the , new() being the addition) to your Deserialize method, since XmlSerializer demands a public parameterless constructor.


Nothing worked here for me

What worked is to MAKE SURE that the C# Class (main class) you are trying to map/deserialize the xml string to HAS AN XmlRootAttribute that matches the root element of the response.

Check my full answer with an exmaple https://stackoverflow.com/a/61525536/1594274


I found doing the following fixed this for me

if (elem.Attribute(XNamespace.Xmlns + "xsi") == null) {    elem.Add(new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"));}if (elem.Attribute(XNamespace.Xmlns + "xsd") == null) {    elem.Add(new XAttribute(XNamespace.Xmlns + "xsd", "http://www.w3.org/2001/XMLSchema"));}