【问题标题】:Facing issue with deserializing the xml to object面临将 xml 反序列化为对象的问题
【发布时间】:2018-06-28 20:11:43
【问题描述】:

我的xml字符串如下

"<?xml version=\"1.0\" encoding=\"utf-8\"?><p:Msg xmlns:tns=\"http://xyx.com\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.xyx.com/location/921.xsd\"><Header><Type>P:B2</Type><UserID>MARKISCOOL</UserID><MsgID>4213</MsgID>
</Header><Data><StatusRecord><TimestampUTCCurrent hex=\"40B18261\" intermediate=\"1085375073\" uom=\"\">2016-01-20T06:05:55Z</TimestampUTCCurrent>
<FileType hex=\"00002765\" intermediate=\"10003\" uom=\"\">10003</FileType>
</StatusRecord></Data></p:Msg>"

我必须将这个 xml 字符串反序列化为一个对象,如图所示

[XmlRoot(Namespace = "http://xyx.com", ElementName = "Msg",  IsNullable = true)]
           public class Info
            {
                public string Type { get; set; }
                public string UserID { get; set; }
                [XMlElement("MsgID")]
                public int MessageId { get; set; }
                public string TimestampUTCCurrent { get; set; }
                public int FileType { get; set; }
            }

我正在尝试将 xml 字符串反序列化为 Info 类,但我将 null 值放入 Info 类。我不确定为什么 xml 中的值没有复制到“Info”对象中。

 public Info Deserialize(string xmlString)
    {
        XDocument doc = XDocument.Parse(xmlString);
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(Info));

        using (var reader = doc.Root.CreateReader())
        {
            return (Info)xmlSerializer.Deserialize(reader);
        }

    }

【问题讨论】:

  • I am getting error 。为什么不与我们分享错误?最高机密?
  • System.Xml.XmlException: '根级别的数据无效。第 1 行,位置 1。'

标签: c# xml xml-parsing xml-deserialization


【解决方案1】:

最好使用 Xml Linq 来完成。下面的代码经过测试:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                           "<p:Msg xmlns:tns=\"http://xyx.com\" xmlns:p=\"http://www.xyx.com/location/921.xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
                              "<Header>" +
                                 "<Type>P:B2</Type>" +
                                 "<UserID>MARKISCOOL</UserID>" +
                                 "<MsgID>4213</MsgID>" +
                              "</Header>" +
                              "<Data>" +
                                 "<StatusRecord>" +
                                    "<TimestampUTCCurrent hex=\"40B18261\" intermediate=\"1085375073\" uom=\"\">2016-01-20T06:05:55Z</TimestampUTCCurrent>" +
                                    "<FileType hex=\"00002765\" intermediate=\"10003\" uom=\"\">10003</FileType>" +
                                 "</StatusRecord>" +
                              "</Data>" +
                           "</p:Msg>";
            XDocument doc = XDocument.Parse(input);
            XElement msg = doc.Root;
            XNamespace ns = msg.GetDefaultNamespace();
            XNamespace pNs = msg.GetNamespaceOfPrefix("p");

            Info info = doc.Elements(pNs + "Msg").Select(x => new Info()
            {
                Type = (string)x.Descendants(ns + "Type").FirstOrDefault(),
                UserID = (string)x.Descendants(ns + "UserID").FirstOrDefault(),
                MessageId = (int)x.Descendants(ns + "MsgID").FirstOrDefault(),
                TimestampUTCCurrent = (string)x.Descendants(ns + "TimestampUTCCurrent").FirstOrDefault(),
                FileType = (int)x.Descendants(ns + "FileType").FirstOrDefault()
            }).FirstOrDefault();

        }

    }
    public class Info
    {
        public string Type { get; set; }
        public string UserID { get; set; }
        public int MessageId { get; set; }
        public string TimestampUTCCurrent { get; set; }
        public int FileType { get; set; }
    }
}

【讨论】:

  • 我修复了命名空间的一些问题。 xsi:schemaLocation 和命名空间之间缺少一个空格应该有 xmlns。
  • 我尝试使用 xmlSerializer.Deserialize(reader); 直接反序列化错误消息为“InvalidOperationException: 不是预期的。”
  • xmlns 需要一个冒号命名空间,然后是我的代码中的等号。您还需要一个“p”命名空间。
  • 这段代码可以正常工作。但不是遍历每个 xelement。我们可以将 xml 属性放在类中属性的顶部,以便在尝试反序列化 xml 中的值时将其复制到类属性中
  • 不!一个类只会得到 Msg 的子元素。您的 xml 有孩子、孙子和曾孙。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多