我有
<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>
和班级
public class ChemieComponent
{
public string Name { get; set; }
public string Id { get; set; }
public double MolarMass { get; set; }
}
我可以使用LINQ查询将此组件解析为对象吗?怎么样?我最终应该有一个IEnumerable,对吧?
修改
<Points>
<Point name="P1" pressure="1">
<Fractions>
<Fraction id="aaa" value="0.15272159"/>
<Fraction id="bbb" value="0.15272159"/>
</Fractions>
more points...
</Points>
答案 0 :(得分:22)
您可以使用以下内容:
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")
};
修改强>
也可以使用Linq访问嵌套元素到Xml:
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"),
}
};
}
答案 1 :(得分:4)
我意识到这是一篇旧帖子,但我最近在对象中读取了一些XML。我并不热衷于Linq to XML - 它不具有可读性,依赖于&#34;魔术字符串&#34;,并且如果XML模式发生变化,则需要更改代码。
对于任何感兴趣的人,我使用XmlSerializer
类将XML反序列化为对象。将反序列化代码放入一个通用的帮助器方法中,然后将一行代码反序列化为一个对象图: -
using System.IO;
using System.Xml.Serialization;
class Program
{
static void Main(string[] args)
{
var xml = @"<?xml version='1.0' encoding='utf-8' ?>
<Model>
<Points>
<Point name='P1' pressure='1'>
<Fractions>
<Fraction id='aaa' value='0.15272159'/>
<Fraction id='bbb' value='0.15272159'/>
</Fractions>
</Point>
</Points>
</Model>";
var model = DeserializeObject<Model>(xml);
}
private static T DeserializeObject<T>(string xml)
{
var serializer = new XmlSerializer(typeof(T));
using (var tr = new StringReader(xml))
{
return (T)serializer.Deserialize(tr);
}
}
}
public class Model
{
[XmlArrayItem("Point")]
public Point[] Points { get; set; }
}
public class Point
{
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
[XmlAttribute(AttributeName = "pressure")]
public int Pressure { get; set; }
[XmlArrayItem("Fraction")]
public Fraction[] Fractions { get; set; }
}
public class Fraction
{
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
[XmlAttribute(AttributeName = "value")]
public double Value { get; set; }
}
有几点需要注意: -
此解决方案显然依赖于使用XML属性修饰域模型,这些属性可能是您可能接受的,也可能是不可接受的。该解决方案还依赖于XML中存在的外部元素,如果根元素是一个数组(在此示例中为<Points>
),它将无法工作。
无法指定IEnumerable<>
(OP提到的);您可以使用数组或List<>
。