a tgq
a tgq

Reputation: 3

How to Deserialize XML document?

How to Deserialize XML document My XML

<Title>
  <NAME>ABC</NAME>
  <BODY>
     <A>IAG4</A>
     <B>
        <B1>
           <C>100</C>  
           <C>EC0001</C1>
           <C2>DEF</C2> 
           <C3>100</C3> 
           <C4>200</C4> 
           <C5>600</C5> 
           <C6>1000</C6> 
        </B1>
        <B1>
           <D>101</D>  
           <D1>EC0002</D1>
        </B1>
     </B>
  </BODY>
</Title>

I want to deserialize this into a class and I want to access them with the objects of the class created. I am using C#.

Upvotes: 0

Views: 70

Answers (2)

jdweng
jdweng

Reputation: 34421

Use xml serialization code as shown below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(Message));
            Message message = (Message)serializer.Deserialize(reader);
        }
    }
    [XmlRoot("MESSAGE")]
    public class Message
    {
        [XmlElement("NAME")]
        public string name { get; set; }
        [XmlElement("BODY")]
        public Body body { get; set; }
    }
    public class Body
    {
        [XmlElement("EQPID")]
        public string eqpid { get; set; }

        [XmlArray("ECS")]
        [XmlArrayItem("EC")]
        public List<EC> ec { get; set; }
    }
    public class EC
    {
        public int ECID { get; set; }  
        public string ECNAME { get; set; }
        public string ECDEF { get; set; } 
        public int ECSLL { get; set; } 
        public int ECSUL { get; set; } 
        public int ECWLL { get; set; }
        public int ECWUL { get; set; } 

    }
}

Upvotes: 1

Anu Viswan
Anu Viswan

Reputation: 18163

You could use online tools like Xml2Csharp to generate classes needed to store your XML Data. To deserialize the Xml, you could then use the XmlSerializer

XmlSerializer serializer = new XmlSerializer(typeof(MESSAGE));
using (TextReader reader = new StringReader(xml))
{
    var result = (MESSAGE)serializer.Deserialize(reader);
}

Where your classes are defined as

[XmlRoot(ElementName="EC")]
public class EC 
{
        [XmlElement(ElementName="ECID")]
        public string ECID { get; set; }
        [XmlElement(ElementName="ECNAME")]
        public string ECNAME { get; set; }
        [XmlElement(ElementName="ECDEF")]
        public string ECDEF { get; set; }
        [XmlElement(ElementName="ECSLL")]
        public string ECSLL { get; set; }
        [XmlElement(ElementName="ECSUL")]
        public string ECSUL { get; set; }
        [XmlElement(ElementName="ECWLL")]
        public string ECWLL { get; set; }
        [XmlElement(ElementName="ECWUL")]
        public string ECWUL { get; set; }
}

[XmlRoot(ElementName="ECS")]
public class ECS 
{
        [XmlElement(ElementName="EC")]
        public List<EC> EC { get; set; }
}

[XmlRoot(ElementName="BODY")]
public class BODY 
{
        [XmlElement(ElementName="EQPID")]
        public string EQPID { get; set; }
        [XmlElement(ElementName="ECS")]
        public ECS ECS { get; set; }
}

[XmlRoot(ElementName="MESSAGE")]
public class MESSAGE 
{
        [XmlElement(ElementName="NAME")]
        public string NAME { get; set; }
        [XmlElement(ElementName="BODY")]
        public BODY BODY { get; set; }
}

Upvotes: 0

Related Questions