I am trying to get the value of an element using a XML attribute criteria. Essentially I just want one value from the entire document and I've been trying to query it directly. This is my XML file:
<?xml version="1.0" encoding="UTF-8"?>
<list version="1.0">
<meta>
<type>resource-list</type>
</meta>
<resources start="0" count="1">
<resource classname="Quote">
<field name="name">Microsoft Corporation</field> <!-- I want this!!! -->
<field name="price">49.869999</field>
<field name="symbol">MSFT</field>
<field name="ts">1461960000</field>
<field name="type">equity</field>
<field name="utctime">2016-04-29T20:00:00+0000</field>
<field name="volume">48411684</field>
</resource>
</resources>
</list>
In particular, I want the field with the "name" attribute in it. This is what I've done to retrieve this:
XDocument xDoc = XDocument.Parse(httpResponseBody);
string name = (string)xDoc.Elements("field").First(x => x.Attribute("name").Value == "name");
I get an "element not matching sequence" error. When I try to experiment with this or change anything, I get an object not set to reference error.
I sense that I've made a simple parsing mistake here, but any help would be appreciated (as well as where I've gone wrong and what I can do to prevent this in the future!)
Thank-you!
string name = xDoc.SelectSingleNode("//field[@name='name']").InnerTextDescendants("field")instead ofElements("field").xDoc.XPathSelectElement(...). There is noSelectSingleNodemethod onXDocument.XmlDocument