I am trying to read an XML file which is 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 am trying to read the read the third value. My Code ables to retrieve the 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 do I need to make in order to read the third or fourth value?
Solution: (Provided by @DGibbs)
XDocument xml = XDocument.Load(sXMLPath);
var elem = (from n in xml.Descendants("FIELD")
where n.Attribute("NAME").Value == "A004"
select n).FirstOrDefault();
MessageBox.Show(elem.Value);