I am parsing an XML document to pull out the first occurrence of “content” node and read the inner text for that node.
The XML file coming in can be seen at:
The code behind that processes the file is:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestToSend);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
string responseString = sr.ReadToEnd();
XmlDocument thisXmlDoc = new XmlDocument();
thisXmlDoc.LoadXml(responseString);
//get Companies
XmlNodeList ocNodesCompany = thisXmlDoc.SelectNodes("//feed/entry/content");
foreach (XmlElement element in ocNodesCompany)
{
description = element.InnerText;
testOutput = "<b>" + stockRow["stockitem_text"].ToString() + "</b>: " + description + "<br /><br />";
break;
}
Response.Write(responseString + "<br /><br />" + testOutput);
Unfortunately even though the XML file is coming through correctly, it is not finding any nodes and therefore returning NULL and not entering the ‘foreach’ loop.
Thanks for any help.
When using XPath queries for elements that are in some namespace, you have to declare and use namespace prefix.
The following code works:
Also, when referring to the root node, you should use
/instead of//.