简体   繁体   English

如何将此 xml 反序列化为对象?

[英]How to deserialize this xml to objects?

I'm fetching data in the format of this:我正在获取以下格式的数据:

<ReplyUserAccount xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" requestid="" version="1.0" xmlns="url">
  <Sender partnerid="xx">xx</Sender>
  <Users>
    <User method="GET" ResultCode="OK" Description="">
      <Guid>xx</Guid>
      <FirstName>xx</FirstName>
      <LastName>xx</LastName>
      <Phone>xx</Phone>
      <Mobile>xx</Mobile>
      <Email>xx</Email>
      <EmplNo>xx</EmplNo>
      <TacPermission />
      <InvPermission>xx</InvPermission>
      <CustomerId>xx</CustomerId>
    </User>
    </Users>
</ReplyUserAccount>

With the following C# objects:使用以下 C# 对象:


    [XmlRoot("ReplyUserAccount")]
    public class ReplyUserAccount
    {
        [XmlElement("Users")]
        public Users Users{ get; set; }
    }

    [XmlType("Users")]
    public class Users
    {
        [XmlElement("User")]
        public List<User> UserList{ get; set; }
    }

    [XmlType("User")]
    public class User
    {
        [XmlElement("EmplNo")]
        public string Id{ get; set; }
        [XmlElement("Guid")]
        public string Guid { get; set; } = null;
        [XmlElement("Email")]
        public string Email { get; set; }
        [XmlElement("FirstName")]
        public string FirstName { get; set; }
        [XmlElement("LastName")]
        public string LastName { get; set; }
        public bool Active { get; set; } = true;
        public string PhoneNumber { get; set; } = null;
    }

And the following deserializing:以及以下反序列化:

var result = await httpClient.GetAsync(url);
var xdoc = XDocument.Parse(await result.Content.ReadAsStringAsync());
XmlSerializer serializer = new XmlSerializer(typeof(ReplyUserAccount));
var content = xdoc.ToString();
TextReader reader = new StringReader(content);
var res = (ReplyUserAccount)serializer.Deserialize(reader);

But I get the following error: InvalidOperationException: <ReplyUserAccount xmlns='xxx'> was not expected .但我收到以下错误: InvalidOperationException: <ReplyUserAccount xmlns='xxx'> was not expected

I'm a little bit lost as to how to properly deserialize this specific xml data.对于如何正确反序列化这个特定的 xml 数据,我有点迷茫。 Any and all help with regards to this is greatly appreciated.非常感谢您对此提供的任何和所有帮助。

To fix the error, You have to remove xmlns and xsi in xml text before deserialize.要修复错误,您必须在反序列化之前删除 xml 文本中的xmlnsxsi You can remove xmlns and xsi like this:您可以像这样删除xmlnsxsi

var content = xdoc.ToString();
string strXMLPattern = @"xmlns(:\w+)?=""([^""]+)""|xsi(:\w+)?=""([^""]+)""";
content = Regex.Replace(content, strXMLPattern, "");

Therefore the method should be as follows因此方法应该如下

var result = await httpClient.GetAsync(url);
var xdoc = XDocument.Parse(await result.Content.ReadAsStringAsync());
XmlSerializer serializer = new XmlSerializer(typeof(ReplyUserAccount));
var content = xdoc.ToString();
string strXMLPattern = @"xmlns(:\w+)?=""([^""]+)""|xsi(:\w+)?=""([^""]+)""";
content = Regex.Replace(content, strXMLPattern, "");
TextReader reader = new StringReader(content);
var res = (ReplyUserAccount)serializer.Deserialize(reader);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM