0

I want to remove some tags from an xml. I'm using selectNodes() but I need to pass the namespace. So I tried to get the namespace by using GetXmlNamespaces(). some of the namespaces do not have a key value. And I can not provide the namespace as a hard coded value and need to change the namespace regularly. The only values I can pass are the tag name and the file. Is there anyway I can get the namespace a tag belongs to in c#?

for example I'm passing this namespace as a hardcoded value but I need to do it programmatically.

        var nsmgr = new XmlNamespaceManager(doc.NameTable);
        nsmgr.AddNamespace("a", "http://schemas.openxmlformats.org/spreadsheetml/2006/main");

        XmlNodeList nodes = doc.SelectNodes("//a:workbook/a:fileVersion", nsmgr);

this namespace 'xmlns="http://schemas.openxmlformats.org/package/2006/relationships"' does not have a key when I try to get it by using

IDictionary<string, string> nameSpaces = GetXmlNamespaces(xmlFilePath);
Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220

2 Answers2

0

Instead of dynamically searching for the used namespace, you can instead find a node without the namespace information.

Have a look at the Q&A's which describe this solution

Rand Random
  • 7,300
  • 10
  • 40
  • 88
0

This can be done using localName refer this question: Select node without namespace with XPath

Someone had commented this and now it's gone but it worked for me. So here I'm posting the same thing