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 8700403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:09:40+00:00 2026-06-13T02:09:40+00:00

How do I read/parse an XML document where the XML namespace alias is unknown?

  • 0

How do I read/parse an XML document where the XML namespace alias is unknown?
The structure and namespaces of the XML document are known, but the alias is not. E.g.

<?xml version="1.0" encoding="utf-8"?>
<Order xmlns:aa="urn:namespace1"
       xmlns:bb="urn:namespace2">
  <aa:Quantity>1</aa:Quantity>
  <bb:Price>9.98</bb:Price>
</Order>

Or

<?xml version="1.0" encoding="utf-8"?>
<Order xmlns:cc="urn:namespace1"
       xmlns:dd="urn:namespace2">
  <cc:Quantity>1</cc:Quantity>
  <dd:Price>9.98</dd:Price>
</Order>

Update: I don’t know the XML namespace aliases up front. They can be whatever.

I need to supply the XmlNamespaceManager with a list of namespaces and alias with the AddNameSpace method like so:

XPathDocument xDoc = new XPathDocument(“Path to my file”); 
XPathNavigator xNav = xDoc.CreateNavigator();
XmlNamespaceManager xmlns = new XmlNamespaceManager(xNav.NameTable);
xmlns.AddNamespace("aa", "urn:namespace1");
xmlns.AddNamespace("bb", "urn:namespace2");

But this is not XML namespace agnostics. My second document uses cc and dd as alias for the same namespace.

  • 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-13T02:09:41+00:00Added an answer on June 13, 2026 at 2:09 am

    The code you have provided is namespace agnostic in the sense that the namespace prefixes used in the source XML does not matter. Given the namespace definitions in your question you have to use the prefixes defined by you in the XPATH, e.g. you have to use aa and bb.

    var quantity = xNav.SelectSingleNode("/Order/aa:Quantity", xmlns);
    

    However, this code will still successfully select from the XML where prefixes cc and dd are used as long as the namespaces urn:namespace1 and urn:namespace2 are correctly used.

    To be able to include namespace prefixes in the XPATH you have to use the overloads that accepts an IXmlNamespaceResolver.

    To reiterate: When you define a namespace using the following code

    xmlns.AddNamespace("aa", "urn:namespace1");
    

    You state that in your code (e.g. in the XPATH you intend to use) you will be using namespace prefix aa for namespace urn:namespace1.

    In the XML you want to parse you assign namespaces using an attribute:

    xmlns:cc="urn:namespace1"
    

    It is important that the string urn:namespace1 matches both places to use that particular namespace. The prefixes are local to your code and the XML file respectively and they do not have to match.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to parse a XML document using Xerces, but I cant seem
I'm trying to read in and parse an xml document in an iPhone app.
I am using XMLParser to parse an xml document that has the following structure:
I would like to read multiple XML files in an array and then parse
I am trying to read an XML file using objective C language and parse
I am trying to parse an XMl document that i received into a string
I need to read an XML formatted document from a C program and extract
I need to read a large XML document from the network and split it
I need to parse the following xml document (which is coming from external web
I'm using an XmlReader.ReadInnerXML to read an XML document (as text) embedded within in

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.