Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

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

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.

XML I am attempting to deserialize

<register-account success="false">
  <user-name>xxxxx</user-name>
  <password>fghgh</password>
  <email>[email protected]</email>
  <error>
    <errorcode>120</errorcode>
    <errormessage>The password is invalid</errormessage>
  </error>
</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; }

    /// <summary>
    /// A Tenant Portal error relating to the RegisterAccountRequest
    /// </summary>
    [XmlElement("error")]
    public QubeError Error;
}

Deserialization Method

    public static T Deserialize<T>(string data) where T : class
    {
        if (data == null)
        {
            return null;
        }

        if (data.Trim().Length == 0)
        {
            return null;
        }

        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.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...