How to parse XML using C# when XML contains same element again? -


i trying read xml file looking like,

<?xml version="1.0" encoding="utf-8"?> <myxml> <session form_id="775938" cid="" id="hakki-laptop_634975758376381105"> <field name="a001date_m" y="2.32" x="5.5" width="7.15" height="0.99">first value</field> <field name="a002" y="2.32" x="17.83" width="2.38" height="0.99">second value</field> <field name="a003" y="1.11" x="17.83" width="2.38" height="0.99">third value</field> <field name="a004" y="1.11" x="5.5" width="2.38" height="0.99">fourth value</field> </session>  </myxml> 

i trying read read third value. code ables retrieve first value.

xmldocument xmldoc = new xmldocument(); xmldoc.load(sxmlpath); xmlnode node = xmldoc.selectsinglenode("myxml/session/field"); if (node != null) {      messagebox.show(node.innertext); } 

what changes need make in order read third or fourth value?

solution: (provided @dgibbs)

xdocument xml = xdocument.load(sxmlpath); var elem = (from n in xml.descendants("field")             n.attribute("name").value == "a004"             select n).firstordefault(); messagebox.show(elem.value); 

use linq xml , select name attribute:

xdocument xml = xdocument.load(filelocation);  var elem = (from n in xml.descendants("field")             n.attribute("name").value == "a004"             select n.value).firstordefault(); 

note need update xml field elements self closing , have closing tags.

example:

<field name="a002" y="2.32" x="17.83" width="2.38" height="0.99" />second value</field> 

should be:

<field name="a002" y="2.32" x="17.83" width="2.38" height="0.99">second value</field> 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -