troubleshooting Question

C# auto-add all namespaces found in XmlNamespaceManager

Avatar of N M
N MFlag for Luxembourg asked on
5 Comments1 Solution223 ViewsLast Modified:
I have the following tiny C# console app which reads an XML and searches a value of a tag using a single XPATH. i don't want to change the way it is implemented (via navigator) but I need a small function to add the namespaces; otherwise i can't use it in many different XML documents. May you help me?
Below what I have (working):

using System;
using System.Xml;
using System.Xml.XPath;

namespace ConsoleApp1
    class Program

        static void Main(string[] args)
            XPathNavigator nav;
            XPathDocument docNav;
            XmlNamespaceManager nsMgr;

            // Openning MYXML doc
            docNav = new XPathDocument(@"c:\Users\Somebody\Desktop\xml\MYLASTXML.123456.MYXML.xml");

            // Creating a navigator to query using XPath.
            nav = docNav.CreateNavigator();

            //Add Namespaces for scb and conf
            nsMgr = new XmlNamespaceManager(nav.NameTable);
            nsMgr.AddNamespace("abc", "");
            nsMgr.AddNamespace("conf", "");
.... //I should continue add namespaces here but I need a function to populate with all namespaces found in the XML document..... can you help?

            nav = docNav.CreateNavigator().SelectSingleNode("/abc:MYXML/blahblahblah/conf:productId[@productIdScheme='']/text()", nsMgr);


Open in new window

Chinmay Patel
Chief Technology Ninja
Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros