Avatar of N M
N MFlag for Luxembourg

asked on 

C# auto-add all namespaces found in XmlNamespaceManager

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", "http://www.ababab.com/MYXML-1");
            nsMgr.AddNamespace("conf", "http://www.fpml.org/FpML-5/confirmation");
.... //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='http://www.fpml.org/coding-scheme/external/instrument-id']/text()", nsMgr);

            Console.WriteLine(nav);
            Console.ReadLine();
        }
    }
}

Open in new window

C#XML

Avatar of undefined
Last Comment
Chinmay Patel
ASKER CERTIFIED SOLUTION
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of N M
N M
Flag of Luxembourg image

ASKER

Thank you very much, I will try to implement but I have difficulties. if you could share a complete solution based on the code published, would be great. Thanks indeed !
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Sure. I am on the move so it might take couple of hours. If you can post the challenge you are facing I might be able to help.
Avatar of N M
N M
Flag of Luxembourg image

ASKER

I tried to include this
 nav.MoveToFollowing(XPathNodeType.Element);
IDictionary<string, string>  namespaces = nav.GetNamespacesInScope(XmlNamespaceScope.All);
immediately after
nav = docNav.CreateNavigator();
but I couldn't compile. At the same time, I was trying to create a function for parsing all namespaces but the "nav" component cannot be called from this function.
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Hi  N M,

I used the same code as I have posted and it worked. Please post the entire snippet so we can figure out why it is giving you an error.

Regards,
Chinmay.
C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo