Question

How to fetch XML data with 'select single node'?

I want to fetch the highlighted field in the image.

On selectSingleNode's xpath, how do I define a name space?

Comments

Keep up to date on this post and subscribe to comments

October 30, 2019 - 4:09am

Provide namespace values in xpath string only.

November 5, 2019 - 9:30pm
Response to Jayachandra Reddy K

I already tried, and it says that the name space is not defined.

November 6, 2019 - 12:34am

Try with below code.

var xmlDoc = new XmlDocument();
xmlDoc.Load(@"filename");
var namespaceName = "ns";
var namespacePrefix = string.Empty;
XmlNamespaceManager nameSpaceManager = null;
XmlNodeList nodeList = xmlDoc.SelectNodes(string.Format("/{0}fileresponse/{0}data/{0}folder/{0}pages", namespacePrefix), nameSpaceManager);
if (nodeList != null)
{
foreach (XmlNode childNode in nodeList)
{
string first = childNode.ChildNodes[0].ChildNodes[1].ChildNodes[1].InnerText;
}
}

below is sample xml:

<fileresponse>
<data>
<filename>abc</filename>
<filetype>1</filetype>
<folder>
 <name>pega</name>
 <documentid>123</documentid>
 <pages>
  <NS1:Page xmlns:NS1="http://b.ws">
  <NS1:Deleted>yes</NS1:Deleted>
  <NS1:Id>
   <NS1:Id>3131231232</NS1:Id>
   <NS1:RefId>12refid</NS1:RefId>
  </NS1:Id>
  <NS1:PageNumber>1</NS1:PageNumber>
  </NS1:Page>
 </pages>
</folder>
</data>
</fileresponse>

 

November 13, 2019 - 10:24pm
Response to Jayachandra Reddy K

Hi Jayachandra,

Thanks for the work-around I'll check and confirm.
I was just wondering why can't I use 'select single node' method since xpath can handle namespaces as well. Could it be a limitation of the method?