[RESOLVED] help with SelectSingleNode() of XmlDocument
I am trying to use the SelectSingleNode function, but it is not returning anything. My code is as follows:
Code:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"c:\sample.xml");
XmlNode node = xmlDoc.SelectSingleNode("//Response/ResponseRecord");
The xml looks like this:
HTML Code:
<?xml version="1.0" encoding="utf-8"?>
<Response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Prod_Name="ProductX" Version="1.0.0.1" TimeStamp="8/14/2007 2:56:45 PM">
<ResponseRecord xmlns="urn:MyService">
<recordID>0</recordID>
</ResponseRecord>
</Response>
I think it may have something to do with the "xmlns".
Re: help with SelectSingleNode() of XmlDocument
Actually I think it has more to do with Response being the root node. By telling it to look for "//Repsonse", you're asking it to go back to the Root node (which is Response) and look for the Response node (which doesn't exist).... Because an XML document can only have one root node, it's implied. Try just .SelectSingleNode("ResponseRecord") ... it should work.
-tg
Re: help with SelectSingleNode() of XmlDocument
I tried it, but unfortunately, it still returns nothing. I'm pretty sure though that it has something to do with the "xmlns" because when I remove the xmlns part, the code I posted above works fine. Unfortunately I can't remove the xmnls as a way to solve the problem.
Re: help with SelectSingleNode() of XmlDocument
Google 'xpath namespace' and I think you will find a couple of articles that talk about your question.
Re: help with SelectSingleNode() of XmlDocument
It's the default "urn:MyService" namespace on the ResponseRecord node:
Code:
XmlNamespaceManager nsManager = new XmlNamespaceManager(xmlDoc.NameTable);
nsManager.AddNamespace("ms", "urn:MyService");
XmlNode node = xmlDoc.SelectSingleNode("Response/ms:ResponseRecord", nsManager);