Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / March 2007

Tip: Looking for answers? Try searching our database.

Xpath query fails to return a node

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Amir - 15 Mar 2007 17:32 GMT
Hi

I am trying to extract a node from the following XML doc

<?xml version='1.0' encoding='UTF-8'?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <soap:Body>
   <getFileContentsResponse xmlns="http://samplesoap.cbis.infocomp.com">
     <out>
       <InfoCompFiles>
         <InfoCompFile>
           <Content>Hello world!!!</Content>
         </InfoCompFile>
       </InfoCompFiles>
     </out>
   </getFileContentsResponse>
 </soap:Body>
</soap:Envelope>

I am after the <out> tag and I am using the following code:

               Dim nsMgr As XmlNamespaceManager = New
XmlNamespaceManager(resultXml.NameTable)
               nsMgr.AddNamespace("soap",
"http://schemas.xmlsoap.org/soap/envelope/")
               nsMgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema")
               nsMgr.AddNamespace("xsi",
"http://www.w3.org/2001/XMLSchema-instance")
               nsMgr.AddNamespace(String.Empty,
"http://samplesoap.cbis.infocomp.com")

               Dim node As XmlNode = resultXml.SelectSingleNode("//out",
nsMgr)

where resultXml contains the above XML document.

Unfortunately, node is alway Nothing!

Any ideas would be much appreciated!
Martin Honnen - 15 Mar 2007 17:49 GMT
>                 nsMgr.AddNamespace(String.Empty,
> "http://samplesoap.cbis.infocomp.com")

Although the XML has no prefix for that namespace for XPath you need to
use one here e.g.

                nsMgr.AddNamespace("cb",
"http://samplesoap.cbis.infocomp.com")

and here e.g.

>                 Dim node As XmlNode = resultXml.SelectSingleNode("//out",
> nsMgr)

Dim node As XmlNode = resultXml.SelectSingleNode("//cb:out", nsMgr)

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

Amir - 15 Mar 2007 18:24 GMT
Martin you are a star! Thanks!

Where does the prefix "cb" come from? I have been looking at this for the
past 2 hours!

> >                 nsMgr.AddNamespace(String.Empty,
> > "http://samplesoap.cbis.infocomp.com")
[quoted text clipped - 11 lines]
>
> Dim node As XmlNode = resultXml.SelectSingleNode("//cb:out", nsMgr)
Martin Honnen - 15 Mar 2007 18:36 GMT
> Where does the prefix "cb" come from? I have been looking at this for the
> past 2 hours!

You can choose any prefix you like, you just need to use a prefix as
those elements are in a namespace. The XML document uses a default
namespace declaration (e.g.
   xmlns="http://samplesoap.cbis.infocomp.com"
) but XPath 1.0 does not know a default namespace. So with XPath 1.0 to
select elements in a certain namespace you need to bind a prefix to the
namespace URI and use that prefix to qualify element names.

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.