Question Having issues with XPathNavigator

demausdauth

Well-known member
Joined
Mar 24, 2008
Messages
281
Location
Minnesota
Programming Experience
3-5
Hope this is in the correct forum.

I am having trouble with navigating a Soap response with the XPath Navigator. If anyone has ideas or a better way of doing it that would be great.

My main question is: Is it because this is a soap envelope that my Node Iterater will not fill.

VB.NET:
     System.IO.MemoryStream memStream = new System.IO.MemoryStream();
     XmlDocument xDoc = FindingSmartSearch.TraceExtension.XmlResponse;
                        
     xDoc.Save(memStream);
     memStream.Position = 0;


     docNav = new XPathDocument(memStream);
     // Create a navigator to query with XPath.
     nav = docNav.CreateNavigator();
     NodeIter = nav.Select("//DidYouMean");

I am trying to drill down directly to the exact nodes I need rather than having to loop through all the objects. I thought that my syntax was correct but I never get anything in the node iterator. And the node does exist and there is a value in. I have seen it while stepping through in debug mode.

VB.NET:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <getSearchResponse xmlns="namespaceuri">
          <searchResponse>
             <AppliedFilters>
               <Filter sequence="1" keywordInterface="Standard" displayStyle="Top">
                  <FilterStyle>Keyword</FilterStyle>
                  <FilterDescription>Keyword</FilterDescription>
                  <FilterValue sequence="1" displayStyle="Top">
                     <Description>past nots</Description>
                     <Value>past nots</Value>
                     <DidYouMean>post notes</DidYouMean>
                  </FilterValue>
               </Filter>
            </AppliedFilters>
         </searchResponse>
      </getSearchResponse>
   </soapenv:Body>
</soapenv:Envelope>

I have tried multiple values for the XPath to no avail. In debug mode I can use the .MoveToFirstChild() and .MoveToNext() and even .MoveToChild() methods to navigate to the correct node, but I still can't get the node iterator to fill.
 
Well, I was not able to get the XPathNavigator to work. I tried using the Namespace manager but was unsuccessful in finding one that would work with the methods I was using. I did eventually end up using the webservice response object and looped through it to find the tags that I needed.
 
Is there a reason you're not referencing the web service and using the proxy classes?

The first code you posted is C# by the way, you have to use VB.Net code here.
 
Sorry about the C#. I thought I had posted to the other VB Forum, where the ASP.Net section is more lenient about the code. :)

The intention was to hopefully save some processing time by going directly to the node in the xml, rather than looping through hundreds of other nodes just to get couple pieces of information.
 
Soap usually means web service and Studio has tools to make them easy to interact with. Here's an example related to the article Implementing The Google Web Service In VB.NET
VB.NET:
Dim web As New ServiceReference1.GoogleSearchPortClient
Dim key As String = "tGCTJkYos3YItLYzI9Hg5quBRY8bGqiM"
Dim didyoumean As String = web.doSpellingSuggestion(key, "past nots")
 
VB.NET:
Dim SearchResponse as New US.getSearchResponse
Dim SearchRequest as New US.getSearchRequest
Dim Send as New US.SoapBinding

SearchResponse = Send.getSearchRequest(SearchRequest)

I'm pretty sure that I get what you're driving.

Essentially that is the process minus the filling of the actual objects (even made it VB for ya :) ). In the webservice we are using, the response object we get back is often a fairly large object (I posted about 2% of the whole object). There is only one searchResponse, however there are AppliedFilters(), Filter(), and the FilterValue(). So to discover all the possible DidYouMeans I needed to loop through those arrays, unfortunately they don't supply any methods to get just the DidYouMean information.
 
To read the above xml document with xpath methods you can use a namespace manager, for example like this:
VB.NET:
Dim nsm As New Xml.XmlNamespaceManager(nav.NameTable)
nsm.AddNamespace("ns", "namespaceuri")
nsm.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/")

Dim NodeIter = nav.Select("//ns:DidYouMean", nsm)
While NodeIter.MoveNext
    Dim s = NodeIter.Current.Value

End While
 
Thanks John that is great that did the trick. However if you got a time for another question.

I understand the "soapenv" namespace but you also added one for ns and then referred to "ns: DidYouMean". I don't see "ns" in the response object xml, is that a default?
 
I understand the "soapenv" namespace but you also added one for ns and then referred to "ns: DidYouMean". I don't see "ns" in the response object xml, is that a default?
It was specified for getSearchResponse node and childs:
<getSearchResponse xmlns="namespaceuri">
 
What I mean is that the Envelope and Body tags have the "soapenv" prefix but the rest of the tags have no prefix. So is the "ns" prefix just assumed?
 
Thanks John, got my own questions answered. After I posted I did some searching on xmlnamespacemanager and default namespace and got some pretty good ideas of what is going on. Thanks again for all the help you gave me.
 
Back
Top