c# - Reading XML file by name -


i have xml file in 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 section. if notice name="macrosample_head1_macro907". trying read value of that. 7.0. have tried using linq

string value7 = (string)(xelement.load("http://b-dvm-4/current")      .descendants().firstordefault(d => d.name.localname == "macrosample_head1_macro907")); 

but have got wrong?

you're trying find element name of macrosample_head1_macro907 - whereas believe want find element named partcount attribute called name 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 might want cast decimal instead of using value - presumably is decimal value.)

note namespace declared default namespace root element:

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

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -