Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deserialize xml using Linq?

Tags:

c#

xml

linq

How can i deserialize this xml using Linq? I want to create List<Step>

<MySteps>
  <Step>
    <ID>1</ID>
    <Name>Step 1</Name>
    <Description>Step 1 Description</Description>
  </Step>
  <Step>
    <ID>2</ID>
    <Name>Step 2</Name>
    <Description>Step 2 Description</Description>
  </Step>
  <Step>
    <ID>3</ID>
    <Name>Step 3</Name>
    <Description>Step 3 Description</Description>
  </Step>
  <Step>
    <ID>4</ID>
    <Name>Step 4</Name>
    <Description>Step 4 Description</Description>
  </Step>
</MySteps>
like image 746
user829174 Avatar asked Feb 28 '12 15:02

user829174


3 Answers

string xml = @"<MySteps>
                <Step>
                    <ID>1</ID>
                    <Name>Step 1</Name>
                    <Description>Step 1 Description</Description>
                </Step>
                <Step>
                    <ID>2</ID>
                    <Name>Step 2</Name>
                    <Description>Step 2 Description</Description>
                </Step>
                <Step>
                    <ID>3</ID>
                    <Name>Step 3</Name>
                    <Description>Step 3 Description</Description>
                </Step>
                <Step>
                    <ID>4</ID>
                    <Name>Step 4</Name>
                    <Description>Step 4 Description</Description>
                </Step>
                </MySteps>";

XDocument doc = XDocument.Parse(xml);

var mySteps = (from s in doc.Descendants("Step")
               select new 
                {
                    Id = int.Parse(s.Element("ID").Value),
                    Name = s.Element("Name").Value,
                    Description = s.Element("Description").Value
                }).ToList();

Heres how you would do it using LINQ. Obviously you should be doing your own error checking.

like image 57
Umair Avatar answered Sep 22 '22 12:09

Umair


LINQ-to-XML is your answer.

List<Step> steps = (from step in xml.Elements("Step")
                    select new Step()
                    {
                        Id = (int)step.Element("Id"),
                        Name = (string)step.Element("Name"),
                        Description = (string)step.Element("Description")
                    }).ToList();

And a bit about doing the conversions from XML from Scott Hanselman

like image 30
Ocelot20 Avatar answered Sep 22 '22 12:09

Ocelot20


Showing the above answers in LINQ method syntax

Descendants:

var steps = xml.Descendants("Step").Select(step => new
{
    Id = (int)step.Element("ID"),
    Name = step.Element("Name").Value,
    Description = step.Element("Description").Value
});

Elements:

var steps2 = xml.Element("MySteps").Elements("Step").Select(step => new
{
    Id = (int)step.Element("ID"),
    Name = step.Element("Name").Value,
    Description = step.Element("Description").Value
});
like image 29
Sharpiro Avatar answered Sep 20 '22 12:09

Sharpiro