Question

I have an XML file in the following format.

<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>

This is just a section. but if you notice the name="MacroSample_Head1_Macro907". I am trying to read the value of that. Which is 7.0. I have tried using linq

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

But I have got something wrong?

Was it helpful?

Solution

You're currently trying to find an element name of MacroSample_Head1_Macro907 - whereas I believe you really want find an element named PartCount with an attribute called name with a value of 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;

(Or you might want to cast to decimal instead of using Value - presumably it is a decimal value.)

Note that the namespace is declared as the default namespace by the root element:

<MTConnectStreams ... xmlns="urn:mtconnect.org:MTConnectStreams:1.2" ...>
    ...
</MTConnectStreams>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top