将xml反序列化为对象时遇到的问题

时间:2018-06-28 20:11:44

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

我的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类,但是我在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);
        }

    }

1 个答案:

答案 0 :(得分:0)

最好用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; }
    }
}