XPath, she don't workey for me

N

not_a_commie

In the code below I'm trying to use an XPath to get the element
containing "boohaha". What would that XPath look like? My experiments
with the namespace manager don't seem to help much.

var xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<soap12:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-
instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""
xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope"">
<soap12:Header>
<NDTSoapHeader xmlns=""http://tempuri.org/"">
<AppPersonID>int</AppPersonID>
<UserGuid>guid</UserGuid>
<AuthTicket>
<xsd:schema>schema</xsd:schema>xml</AuthTicket>
<AgencyOfCurrentUser>string</AgencyOfCurrentUser>
<InvalidUser>boolean</InvalidUser>
<WSTime>double</WSTime>
</NDTSoapHeader>
<NDTSoapHeader xmlns=""http://tempuri.org/"">
<AppPersonID>int</AppPersonID>
<UserGuid>guid</UserGuid>
<AuthTicket></AuthTicket>
<AgencyOfCurrentUser>string</AgencyOfCurrentUser>
<InvalidUser>boolean</InvalidUser>
<WSTime>double</WSTime>
</NDTSoapHeader>
</soap12:Header>
<soap12:Body>
<JBQueueResponse xmlns=""http://tempuri.org/"">
<JBQueueResult>boohaha</JBQueueResult>
</JBQueueResponse>
</soap12:Body>
</soap12:Envelope>";

var reader = XmlReader.Create(new StringReader(xml));
var xd = XDocument.Load(reader);
var mgr = new XmlNamespaceManager(reader.NameTable);
mgr.AddNamespace("s", "soap12:http://www.w3.org/2003/05/soap-
envelope");
mgr.AddNamespace("j", "http://tempuri.org/");
var ret = xd.XPathSelectElement("//s:Envelope/s:Body/j:JBQueueResponse/
j:JBQueueResponse", mgr);
 
P

Pavel Minaev

In the code below I'm trying to use an XPath to get the element
containing "boohaha". What would that XPath look like? My experiments
with the namespace manager don't seem to help much.
<soap12:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-
instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""
xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope"">
  <soap12:Header>
    <NDTSoapHeader xmlns=""http://tempuri.org/"">
      <AppPersonID>int</AppPersonID>
      <UserGuid>guid</UserGuid>
      <AuthTicket>
        <xsd:schema>schema</xsd:schema>xml</AuthTicket>
      <AgencyOfCurrentUser>string</AgencyOfCurrentUser>
      <InvalidUser>boolean</InvalidUser>
      <WSTime>double</WSTime>
    </NDTSoapHeader>
    <NDTSoapHeader xmlns=""http://tempuri.org/"">
      <AppPersonID>int</AppPersonID>
      <UserGuid>guid</UserGuid>
      <AuthTicket></AuthTicket>
      <AgencyOfCurrentUser>string</AgencyOfCurrentUser>
      <InvalidUser>boolean</InvalidUser>
      <WSTime>double</WSTime>
    </NDTSoapHeader>
  </soap12:Header>
  <soap12:Body>
    <JBQueueResponse xmlns=""http://tempuri.org/"">
      <JBQueueResult>boohaha</JBQueueResult>
    </JBQueueResponse>
  </soap12:Body>
</soap12:Envelope>";

var reader = XmlReader.Create(new StringReader(xml));
var xd = XDocument.Load(reader);
var mgr = new XmlNamespaceManager(reader.NameTable);
mgr.AddNamespace("s", "soap12:http://www.w3.org/2003/05/soap-
envelope");
mgr.AddNamespace("j", "http://tempuri.org/");
var ret = xd.XPathSelectElement("//s:Envelope/s:Body/j:JBQueueResponse/
j:JBQueueResponse", mgr);

I see at least two problems with your code.

First, this:

mgr.AddNamespace("s", "soap12:http://www.w3.org/2003/05/soap-
envelope");

What is "soap12:" doing in that namespace URI? You get a wrong
namespace that way. It should be just "http:".

Second, this:

"//s:Envelope/s:Body/j:JBQueueResponse/j:JBQueueResponse"

you probably meant

"//s:Envelope/s:Body/j:JBQueueResponse/j:JBQueueResult"
 
N

not_a_commie

Thank you for your help. With your tips I was able to make that work.
After looking around online more this morning, I ended up using the
local-name function to avoid the namespace stuff altogether. One thing
that is confusing is that the NameTable put into the manager contains
those namespace strings already. However, querying that table for the
namespace prefixes returns null.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top