按名称读取XML文件

时间:2013-05-09 20:46:29

标签: c# xml linq-to-xml

我有一个格式如下的XML文件。

<ComponentStream component="Path" name="path" componentId="DV005120211_40"><Samples><PathFeedrate dataItemId="DV005120211_47" timestamp="2013-05-14T02:26:12.0576" name="path_feedrate1" sequence="8608400">1440.0000000000</PathFeedrate><PathFeedrate dataItemId="DV005120211_48" timestamp="2013-05-09T19:30:45.0389Z" name="path_feedrate2" sequence="216">UNAVAILABLE</PathFeedrate><PathFeedrate dataItemId="DV005120211_49" timestamp="2013-05-09T19:30:45.0389Z" name="path_feedrate3" sequence="217">UNAVAILABLE</PathFeedrate><PathFeedrate dataItemId="DV005120211_50" timestamp="2013-05-09T19:30:45.0389Z" name="path_feedrate4" sequence="219">UNAVAILABLE</PathFeedrate><PartCount dataItemId="DV005120211_62" timestamp="2013-05-13T22:11:00.0639" name="MacroSample_Head1_Macro910" sequence="8099806">0.0000000000</PartCount><PartCount dataItemId="DV005120211_63" timestamp="2013-05-13T22:11:00.0639" name="MacroSample_Head1_Macro909" sequence="8099805">0.0000000000</PartCount><PartCount dataItemId="DV005120211_64" timestamp="2013-05-13T22:11:00.0639" name="MacroSample_Head1_Macro908" sequence="8099804">0.0000000000</PartCount><PartCount dataItemId="DV005120211_65" timestamp="2013-05-13T22:11:00.0639" name="MacroSample_Head1_Macro907" sequence="8099803">7.0000000000</PartCount><PartCount dataItemId="DV005120211_66" timestamp="2013-05-13T22:11:00.0639" name="MacroSample_Head1_Macro906" sequence="8099802">0.0000000000</PartCount><PartCount dataItemId="DV005120211_67" timestamp="2013-05-13T22:11:00.0639" name="MacroSample_Head1_Macro905" sequence="8099801">0.0000000000</PartCount><PartCount dataItemId="DV005120211_68" timestamp="2013-05-13T22:11:00.0639" name="MacroSample_Head1_Macro904" sequence="8099800">0.0000000000</PartCount><PartCount dataItemId="DV005120211_69" timestamp="2013-05-13T22:11:00.0639" name="MacroSample_Head1_Macro903" sequence="8099799">0.0000000000</PartCount><PartCount dataItemId="DV005120211_70" timestamp="2013-05-13T22:11:00.0639" name="MacroSample_Head1_Macro902" sequence="8099798">0.0000000000</PartCount><PartCount dataItemId="DV005120211_71" timestamp="2013-05-13T22:11:00.0639" name="MacroSample_Head1_Macro901" sequence="8099797">0.0000000000</PartCount>

这只是一个部分。但如果您注意到名称=&#34; MacroSample_Head1_Macro907&#34;。我试图读取它的价值。这是7.0。 我尝试过使用linq

string value7 = (string)(XElement.Load("http://B-DVM-4/current")
     .Descendants().FirstOrDefault(d => d.Name.LocalName == "MacroSample_Head1_Macro907"));

但我有错误吗?

1 个答案:

答案 0 :(得分:3)

您目前正在尝试查找元素名称MacroSample_Head1_Macro907 - 而我相信您确实希望找到一个名为PartCount且带有属性的元素名为name,其值为MacroSample_Head1_Macro907

var element = XElement.Load("http://B-DVM-4/current");
XNamespace ns = "urn:mtconnect.org:MTConnectStreams:1.2";
var part = element.Descendants(ns + "PartCount")
                  .First(x => (string) x.Attribute("name") == 
                              "MacroSample_Head1_Macro907")
                  .Value;

(或者您可能希望转换为decimal而不是使用Value - 可能是 小数值。)

请注意,命名空间由根元素声明为默认命名空间:

<MTConnectStreams ... xmlns="urn:mtconnect.org:MTConnectStreams:1.2" ...>
    ...
</MTConnectStreams>