[英]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.