Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8516637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:29:03+00:00 2026-06-11T05:29:03+00:00

I’m trying to parse a valid XML document in c# .NET using xPathNavigator .

  • 0

I’m trying to parse a valid XML document in c# .NET using xPathNavigator. The start of the XML document looks like this:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <ns1:myResponse xmlns:ns1="ExampleNS" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
            <myReturn href="#2" />

As you can see, there’s some namespaces defined on the root element, but then ns1 is defined later on. When I try and evaluate an xPath query /soapenv:Envelope/soapenv:Body/ns1:myResponse/myReturn I get an XPathException:

Namespace prefix 'ns1' is not defined.

What I’m doing is getting the XML document as a string, loading it into an XmlDocument object. Then I’m creating a navigator, moving to the root element and calling GetNamespacesInScope(XmlNamespaceScope.All). I loop through this collection, which contains the namespaces for xml, soapenv, xsd and xsi as defined on the root, adding them to the namespacemanager. Then I create an xPathNavigator and call Evaluate, and get the exception.

Code is this:

string response = GetXMLString();
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(response);

var xmlDocumentNavigator = xmlDocument.CreateNavigator();
xmlDocumentNavigator.MoveToFollowing(XPathNodeType.Element);
var xnm = new XmlNamespaceManager(xmlDocument.NameTable);
var namespacesInScope = xmlDocumentNavigator.GetNamespacesInScope(XmlNamespaceScope.All);
if (namespacesInScope != null)
{
    foreach (var prefix in namespacesInScope.Keys)
    {
        xnm.AddNamespace(prefix, namespacesInScope[prefix]);
    }
}

var xPathDocument = new XPathDocument(new StringReader(response));
var xPathNavigator = xPathDocument.CreateNavigator();

xPathNavigator.Evaluate("/soapenv:Envelope/soapenv:Body/ns1:myResponse/myReturn", xnm);

Why doesn’t the GetNamespacesInScope method pick up on ns1?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T05:29:05+00:00Added an answer on June 11, 2026 at 5:29 am

    The problem is that GetNamespacesInScope only returns the namespaces in the scope of the navigator’s current node (as I guess the method suggests :).

    One hack of getting all the Namespaces is to walk all the elements, get the namespaces, and then reassemble just the unique ones.

    Dictionary merge code courtesy of JonSkeet

     XPathDocument xpd = new XPathDocument(new StringReader(s));
     XPathNavigator xpn = xpd.CreateNavigator();
     List<IDictionary<string, string>> xmlnsList = new List<IDictionary<string,string>>();
     while (xpn.MoveToFollowing(XPathNodeType.Element))
     {
         xmlnsList.Add(xpn.GetNamespacesInScope(XmlNamespaceScope.All));
     }
     var result = xmlnsList.SelectMany(dict => dict)
                  .ToLookup(pair => pair.Key, pair => pair.Value)
                  .ToDictionary(group => group.Key, group => group.First());
     if (result != null)
     {
         foreach (var prefix in result.Keys)
         {
             xnm.AddNamespace(prefix, result[prefix]);
         }
     }
    

    Edit

    As per Paciv’s comment, if you really don’t know what the namespaces are in an xml document in advance, an alternative is to use namespace agnostic Xpath, and avoid the issues around sniffing out namespaces (and eliminating the need for namespace managers entirely).

    In your example, this would be:

    xPathNavigator.Evaluate("/*[local-name()='Envelope']/*[local-name()='Body']/*[local-name()='myResponse']/*[local-name()='myReturn']");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.