How to read/import XML file contents to C# object?


Some times we just need to read/import XML file contents to C# object.

So let’s see how to do that.

We already know how to save or export C# object data to XML file

Creating XML file

let’s create xml file with some data as follows

XML file
<?xml version="1.0"?>
<ArrayOfEmployee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Employee>
    <Id>1</Id>
    <Name>Venki Chikkanti</Name>
    <Dept>Development</Dept>
  </Employee>
  <Employee>
    <Id>2</Id>
    <Name>Matt</Name>
    <Dept>Management</Dept>
  </Employee>
  <Employee>
    <Id>3</Id>
    <Name>Mark</Name>
    <Dept>Transport</Dept>
  </Employee>
</ArrayOfEmployee>

Next we have to create C# class as follows

C# object
public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Dept { get; set; }
}

Now let’s deserialize XML data and store into C# object and display as follows

Deserializing XML data to C#
static void Main(string[] args)
{
    var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "//XMLData.xml";
    List<Employee> emps = new List<Employee>();
    XmlDocument xmlDocument = new XmlDocument();
    xmlDocument.Load(path);
    string xmlString = xmlDocument.OuterXml;
    using (StringReader read = new StringReader(xmlString))
    {
        Type outType = typeof(List<Employee>);
        XmlSerializer serializer = new XmlSerializer(outType);
        using (XmlReader reader = new XmlTextReader(read))
        {
            emps = (List<Employee>)serializer.Deserialize(reader);
            reader.Close();
        }
        read.Close();
    }
    foreach (var item in emps)
    {
        Console.WriteLine(item.Id + " " + item.Name + " " + item.Dept);
    }
}

 

Deserialization XML to C#

Total Code:

Total Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
    public class Class2
    {
        static void Main(string[] args)
        {
            var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "//XMLData.xml";
            List<Employee> emps = new List<Employee>();
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(path);
            string xmlString = xmlDocument.OuterXml;
            using (StringReader read = new StringReader(xmlString))
            {
                Type outType = typeof(List<Employee>);
                XmlSerializer serializer = new XmlSerializer(outType);
                using (XmlReader reader = new XmlTextReader(read))
                {
                    emps = (List<Employee>)serializer.Deserialize(reader);
                    reader.Close();
                }
                read.Close();
            }
            foreach (var item in emps)
            {
                Console.WriteLine(item.Id + " " + item.Name + " " + item.Dept);
            }
        }
        public class Employee
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Dept { get; set; }
        }
    }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s