Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse xml using LINQ to XML to class objects

Tags:

c#

linq-to-xml

I have

<Model>
    <Components>
        <Component name="a" id="aaa" molarmass="60.05"/>
        <Component name="b" id="bbb" molarmass="18.02"/>
        <Component name="c" id="ccc" molarmass="32.04"/>
        <Component name="d" id="ddd" molarmass="46.03"/>
    </Components>
    ...
</Model>

and the class

public class ChemieComponent
{
    public string Name { get; set; }
    public string Id { get; set; }
    public double MolarMass { get; set; }
}

Can I with the LINQ query parse this components to objects? How? I the end should I have a IEnumerable, right?

EDIT

<Points>
    <Point name="P1" pressure="1">
    <Fractions>
        <Fraction id="aaa" value="0.15272159"/>
        <Fraction id="bbb" value="0.15272159"/>
    </Fractions>
    more points...
 </Points>
like image 288
Pedro Dusso Avatar asked Dec 07 '11 14:12

Pedro Dusso


People also ask

Does LINQ work with XML?

The most important advantage of LINQ to XML is its integration with Language-Integrated Query (LINQ). This integration enables you to write queries on the in-memory XML document to retrieve collections of elements and attributes.

What is the difference between XDocument and XmlDocument?

XDocument is from the LINQ to XML API, and XmlDocument is the standard DOM-style API for XML. If you know DOM well, and don't want to learn LINQ to XML, go with XmlDocument .

Which of the following option is created to retrieve data into XML using LINQ?

The LINQ to XML will bring the XML document into memory and allows us to write LINQ Queries on in-memory XML document to get the XML document elements and attributes. To use LINQ to XML functionality in our applications, we need to add "System. Xml. Linq" namespace reference.


1 Answers

You can use the following:

XDocument doc = XDocument.Parse(xml);
IEnumerable<ChemieComponent> result = from c in doc.Descendants("Component")
                                      select new ChemieComponent()
                                      {
                                          Name = (string)c.Attribute("name"),
                                          Id = (string)c.Attribute("id"),
                                          MolarMass = (double)c.Attribute("molarmass")
                                      };

EDIT

Accessing nested elements with Linq to Xml is also possible:

public class Point 
{
    public string Name { get; set; }
    public int Pressure { get; set; }

    public IEnumerable<Fraction> Fractions { get; set; }
}

public class Fraction
{
    public string Id { get; set; }
    public double Value { get; set; }
}

static void Main()
{
    string xml = @"<Points>
        <Point name='P1' pressure='1'>
            <Fractions>
                <Fraction id='aaa' value='0.15272159'/>
                <Fraction id='bbb' value='0.15272159'/>
            </Fractions>
        </Point>
        </Points>";

    XDocument doc = XDocument.Parse(xml);
    IEnumerable<Point> result = from c in doc.Descendants("Point")
                                select new Point()
                                {
                                    Name = (string)c.Attribute("name"),
                                    Pressure = (int)c.Attribute("pressure"),
                                    Fractions = from f in c.Descendants("Fraction")
                                                select new Fraction() 
                                                {
                                                    Id = (string)f.Attribute("id"),
                                                    Value = (double)f.Attribute("value"),
                                                }
                                };
}
like image 68
Wouter de Kort Avatar answered Sep 23 '22 04:09

Wouter de Kort