使用LINQ to XML来类对象解析XML [英] Parse xml using LINQ to XML to class objects

查看:204
本文介绍了使用LINQ to XML来类对象解析XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <模型与GT; 
<成分>
<成分名称=一个ID =AAA摩尔质量=60.05/>
<成分名称=BID =BBB摩尔质量=18.02/>
<成分名称=CID =CCC摩尔质量=32.04/>
<成分名称=DID =DDD摩尔质量=46.03/>
< /成分>

< /型号:GT;

和类

 公共类ChemieComponent 
{
公共字符串名称{;组; }
公共字符串ID {搞定;组; }
公共双摩尔质量{搞定;组; }
}



我可以用LINQ查询解析该组件的对象?怎么样?我到底我应该有一个IEnumerable的,对吧?



修改



 <积分和GT; 
<点名称=P1压力=1>
<馏分>
<分数ID =AAA值=0.15272159/>
<分数ID =BBB值=0.15272159/>
< /馏分>
更多的积分...
< /分>


解决方案

您可以使用以下方法:

 的XDocument DOC = XDocument.Parse(XML); 
IEnumerable的< ChemieComponent>结果=从C中doc.Descendants(组件)
选择新ChemieComponent()
{
名称=(字符串)c.Attribute(名称),
标识=(字符串)c.Attribute(ID),
摩尔质量=(双)c.Attribute(摩尔质量)
};

修改



访问使用LINQ到XML嵌套的元素也是可能的:

 公共类点
{
公字符串名称{;组; }
公众诠释压力{搞定;组; }

公开的IEnumerable<馏分GT;分数{搞定;组; }
}

公共类分数
{
公共字符串ID {搞定;组; }
公共double值{搞定;组; }
}

静态无效的主要()
{
串XML = @<点>
<点名称='P1'压力='1'>
将;级分>
将级份编号=AAA值='0.15272159'/>
将级份编号=BBB值='0.15272159 />
< /馏分>
< /点和GT;
< /分>中;

的XDocument DOC = XDocument.Parse(XML);
IEnumerable的<点和GT;结果=从C中doc.Descendants(点)
选择新的点()
{
名称=(字符串)c.Attribute(名称),
压力=(INT)c.Attribute(压力),
组分=从f由于c.Descendants(分数)
选择新的分数()
{
n = (字符串)f.Attribute(ID),
值=(双)f.Attribute(值),
}
};
}


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>

解决方案

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

这篇关于使用LINQ to XML来类对象解析XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆