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

后端 未结 3 431
情歌与酒
情歌与酒 2020-11-29 04:55

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.

相关标签:
3条回答
  • 2020-11-29 05:27

    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"));
    }
    
    0 讨论(0)
  • 2020-11-29 05:30

    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

    0 讨论(0)
  • 2020-11-29 05:44

    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.

    0 讨论(0)
提交回复
热议问题