0

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

但是我有什么问题吗?

4

1 回答 1

3

您当前正在尝试查找元素名称MacroSample_Head1_Macro907- 而我相信您确实想要找到一个名为的元素PartCount该元素的名称为:nameMacroSample_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>
于 2013-05-09T20:50:00.900 回答