CodeSmith
CodeSmith

Reputation: 30

How to get 'xpath' value if the root node 2 xmlns url and one xmlns don't have prefix?

I have two xmlns attribute and I try to xpath one node but it is not working

I am using XmlDocument and I am trying to xpath from that xml . it returns null because the root node have two xml attributes.

   <CreateRequest 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns="http://fex.com/ws/openship/v15">
       <WebAuthenticationDetail>
           <Parent>
               <Key/>
               <Password />
           </Parent>
           <UserCredential>
               <Key />
               <Password />
           </UserCredential>
       </WebAuthenticationDetail>
       <ClientDetail>
           <AccountNumber />
           <MeterNumber />
       </ClientDetail> 
   </CreateRequest>

   var nsmgr = new XmlNamespaceManager(xml.NameTable);
   nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");     
   var     
   nodes=xml.SelectNodes("/CreateRequest/ClientDetail/AccountNumber",nsmgr);  

Upvotes: 0

Views: 462

Answers (2)

Alexander Petrov
Alexander Petrov

Reputation: 14251

Your xml has a default namespace. You have to add it, assign it a prefix (I used ns). And then use this prefix in xpath.

Use it as follows:

var nsmgr = new XmlNamespaceManager(xml.NameTable);
nsmgr.AddNamespace("ns", "http://fex.com/ws/openship/v15");
var nodes = xml.SelectNodes("/ns:CreateRequest/ns:ClientDetail/ns:AccountNumber", nsmgr);

Upvotes: 1

Ed Bangga
Ed Bangga

Reputation: 13026

you can ignore the namespaces.

xml.SelectNodes("/*[local-name()='CreateRequest']/*[local-name()='ClientDetail']/*[local-name()='AccountNumber']");

Upvotes: 0

Related Questions