Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

c# - Parse xml using LINQ to XML to class objects

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"),
                                                }
                                };
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...