简体   繁体   English

将 Xml 反序列化为对象时出错 - 不需要 xmlns=''

[英]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.我在尝试反序列化一些 XML 时遇到了真正的麻烦,希望有人能提供一些帮助。 I have read a lot of similar posts but I am unable to resolve this.我已经阅读了很多类似的帖子,但我无法解决这个问题。

XML I am attempting to deserialize我正在尝试反序列化的 XML

<register-account success="false">
  <user-name>xxxxx</user-name>
  <password>fghgh</password>
  <email>test@example.com</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). XML 文档中存在错误 (1, 2)。 ---> System.InvalidOperationException: was not expected. ---> System.InvalidOperationException: 不是预期的。 at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderData.Read5_data()在 Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderData.Read5_data()

Inner Exception as follows:内部异常如下:

<register-account xmlns=''> was not expected.

Simply take off the Namespace = :只需取下Namespace =

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

since your xml doesn't seem to be in an xml-namespace.因为您的 xml 似乎不在 xml 命名空间中。 Also, [Serializable] isn't used by XmlSerializer .此外, XmlSerializer不使用[Serializable]

If your xml was using a namespace it would have an xmlns at the root.如果您的 xml 正在使用命名空间,那么它的根将有一个xmlns

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.此外,为了帮助调用者,您可以将where T : class, new() (该, new()是添加)添加到您的Deserialize方法中,因为XmlSerializer需要一个公共无参数构造函数。

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.有效的是确保您尝试将 xml 字符串映射/反序列化为与响应的根元素匹配的 XmlRootAttribute 的 C# 类(主类)。

Check my full answer with an exmaple https://stackoverflow.com/a/61525536/1594274用一个例子检查我的完整答案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"));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将Xml反序列化为对象时出错-{ <string xmlns='http://tempuri.org/'> 没想到。} - Error Deserializing Xml to Object - {<string xmlns='http://tempuri.org/'> was not expected.} 将 Xml 反序列化为列表<T> - xmlns=&#39;&#39; 不是预期的 - Deserializing Xml to List<T> - xmlns='' was not expected {”<user xmlns=''> 不是预期的。} 反序列化 Twitter XML - {"<user xmlns=''> was not expected.} Deserializing Twitter XML 反序列化为 object 时出现“xmlns=''&gt; 不是预期的”(在某些计算机上) - "xmlns=''> was not expected" while deserializing to object (on some computers) xml文档中有错误(2 2)xmlns =“ - there is an error in xml document (2 2) xmlns='' was not expected 将XML响应转换为C#对象-错误(不期望xmlns =&#39;&#39;&gt;) - Converting XML response to C# object - Error ( xmlns=''> was not expected ) xmlns=&#39;&#39;&gt; 不是预期的。 - DeserializeXml to object 时 XML 文档 (2, 2) 中存在错误 - xmlns=''> was not expected. - There is an error in XML document (2, 2) while DeserializeXml to object XML 反序列化“元素不是预期的”错误 - XML Deserializing "Element was not expected" error xmlns=&#39;&#39;&gt; 不是预期的。 - XML 文档中存在错误 (2, 2) - xmlns=''> was not expected. - There is an error in XML document (2, 2) XML反序列化-不需要xmlns - XML Deserialization - xmlns not expected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM