-1

I am having xml string as follows

"<?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>"

I have to deserialize this xml string into a object as shown

[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; }
            }

I am trying to deserialize xml string into Info class,but I am getting null values into the Info class.I am not sure why values from xml are not copying into 'Info'object.

 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 Answers1

0

This is better done with Xml Linq. Below code is tested :

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; }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • I fixed some issues with the namespaces. There is a space missing between xsi:schemaLocation and a namespace should have xmlns. – jdweng Jun 29 '18 at 00:01
  • I tried to deserialize directly using xmlSerializer.Deserialize(reader); Error Message as "InvalidOperationException: was not expected." – Wolverine1985 Jun 29 '18 at 00:44
  • xmlns requires a colon namespace and then the equal like in my code. You also need a 'p' namespace. – jdweng Jun 29 '18 at 01:17
  • this code is working.But instead of going through each xelement. can we put xml attributes on the top of properties in the class so that when try to deserialize the value from xml will get copied to class properties – Wolverine1985 Jun 29 '18 at 01:34
  • No! A class will only get child elements of Msg. Your xml has children, grand children, and great grand children. – jdweng Jun 29 '18 at 03:06