|
-
Aug 15th, 2007, 10:29 AM
#1
Thread Starter
Frenzied Member
[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".
-
Aug 15th, 2007, 10:58 AM
#2
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
-
Aug 15th, 2007, 04:30 PM
#3
Thread Starter
Frenzied Member
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.
-
Aug 16th, 2007, 08:43 AM
#4
Re: help with SelectSingleNode() of XmlDocument
Google 'xpath namespace' and I think you will find a couple of articles that talk about your question.
-
Aug 18th, 2007, 02:06 AM
#5
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);
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|