Udemy

How to Deserialize Xml using LINQ

Friday, September 12, 2014 0 Comments A+ a-

Binding XML data to Model in C# MVCToday i saw a SO question where OP had xml in a string and OP wanted to deserialize to its specifc type using linq.

Here is the xml:


<people>  
            <person>
                <name>Mr Brown</name>
                <age>40</age>
                <hobby>
                    <title>Eating</title>
                    <description>eats a lot</description>
                </hobby>
                <hobby>
                    <title>Sleeping</title>
                    <description>sleeps a lot</description>
                </hobby>
            </person>
            <person>
                <name>Mr White</name>
                <age>40</age>
                <hobby>
                    <title>Skiing</title>
                    <description>more details on this</description>
                </hobby>
                <hobby>
                    <title>Football</title>
                    <description>watches football</description>
                </hobby>
            </person>
            </people>

First of all we need to create class type in which the data will be deserialized.We have main node people so our one class will be People with three properties Name,Age and Hobbies.As hobbies can be multiple of a person we will need a collection of hobbies and you can observe that from the xml as well that person have multiple hobbies and of course i also have many hobbies..:)


here it is:


public class People
        {
            public string Name { get; set; }
            public string Age { get; set; }
            public IList<Hobby> Hobbies { get; set; }
        }
        public class Hobby
        {
            public string Title { get; set; }
            public string Description { get; set; }
        }

First of all i will load the string xml in to XElement :


string xml = @"<people>  
            <person>
                <name>Mr Brown</name>
                <age>40</age>
                <hobby>
                    <title>Eating</title>
                    <description>eats a lot</description>
                </hobby>
                <hobby>
                    <title>Sleeping</title>
                    <description>sleeps a lot</description>
                </hobby>
            </person>
            <person>
                <name>Mr White</name>
                <age>40</age>
                <hobby>
                    <title>Skiing</title>
                    <description>more details on this</description>
                </hobby>
                <hobby>
                    <title>Football</title>
                    <description>watches football</description>
                </hobby>
            </person>
            </people>";

XElement element=XElement.Parse(xml);

Now we will write linq query to deserialize the xml. Here we go:


var person = from a in element.Descendants("person")
             select new People
             {
              Name = a.Element("name").Value,
              Age = a.Element("age").Value,
              Hobbies = a.Descendants("hobby")
                         .Select(x => new Hobby
                                 {
                                  Title = x.Element("title").Value,
                                  Description = x.Element("description").Value
                                 }).ToList()
                            };
  
foreach(var d in person)
{
  
  Console.WriteLine(String.Format("Name = {0}:Age={1}:Hobbies:{2}",d.Name,d.Age,d.Hobbies.Count()));
  
}

Here is theWorking Example on DOT NET FIDDLE of the code.


Cheers! Happy Coding.

Stackoverflow Reference Question:

Binding XML data to Model in C# MVC

Coursera - Hundreds of Specializations and courses in business, computer science, data science, and more