【发布时间】:2014-02-07 18:21:56
【问题描述】:
我希望你能帮忙:-) 我一直在尝试使用 LINQ 读取 C# 中的 XML 文件。
这是 XML 结构:
<DataBase
xsi:schemaLocation="http://somestuff.new/xml http://somestuff.xsd"
xmlns:ns5="http://somestuff.new/ns5"
xmlns:ns3="http://somestuff.new/ns3"
xmlns:ns2="http://somestuff.new/ns2"
xmlns="http://somestuff.new/ns"
xmlns:xsi="http://somestuff.new/XMLScema-instance"
xmlns:ns4="http://somestuff.new/ns4">
<Cars>
<SmallCars attribute="Something">
<Id>licenceplate</Id>
<Parts attribute="All Parts">
<Extras>
<Gauges xmlns="http://somestuff.new/ns32>
<Speed>100</Speed>
<Rpm>3200</Rpm>
</Gauges>
</Extras>
</Parts>
</SmallCars>
</Cars>
</DataBase>
我想使用 LINQ 从 Speed 和 RPM 中读取值,但我尝试的一切似乎都失败了......
这是我的尝试之一:
XNamespace ns3 = XNamespace.Get("http://somestuff.new/ns3");
from gaugeElement in extentionElement.Descendants(ns3 + "Gauges")
select new Gauge
{
Speed = tpxElement.Element(ns3 + "Speed") != null ? Convert.ToDouble(tpxElement.Element(ns3 + "Speed").Value) : 0.00,
Rpm = tpxElement.Element(ns3 + "Rpm") != null ? Convert.ToInt32(tpxElement.Element(ns3 + "Rpm").Value) : 0
}
我正在使用具有属性的 Gauge 类:
public int Speed { get; set; }
public int Rpm { get; set; }
我希望你们中的一个聪明人能给我提供一个关于如何获得这些值的例子,或者解释为什么我对这些值的追求失败了 :-)
【问题讨论】:
标签: c# xml linq namespaces