Results 1 to 5 of 5

Thread: [RESOLVED] help with SelectSingleNode() of XmlDocument

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Resolved [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".

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    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.

  4. #4
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: help with SelectSingleNode() of XmlDocument

    Google 'xpath namespace' and I think you will find a couple of articles that talk about your question.

  5. #5
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

    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
  •  



Click Here to Expand Forum to Full Width