按名称读取XML文件

本文关键字:XML 文件 读取 | 更新日期: 2023-09-27 18:13:00

我有一个以下格式的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>

这只是一个部分。但如果您注意到名称="MacroSample_Head1_Macro907"。我试着解读它的价值。等于7.0。我试过使用linq

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

但是我做错了什么?

按名称读取XML文件

您目前正试图找到一个元素名称的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>