0

In below Xml, I tried to delete the xmlns attribute but, its unable to populate under, xmlNode.Attributes still appear under OuterXml and final XmlDocument.

... 
<z:Datac knid="2" xmlns="http://services/api/"></z:Datac>
...
<z:Datac knid="3" xmlns="http://services/api/"></z:Datac>
....
<z:Datac knid="5" xmlns="http://services/api/"></z:Datac>
....

How to remove xmlns attribute for each z:Datac element?

foreach (var item in nodes)
{
    var xmlnsAttribute = (item.Attributes has "xmlns" attribute)

    if (xmlnsAttribute != null)
    {
        Remove xmlNode...  //not able to reach here as not able to find xmlns.
    }
}

I don't have xml.linq

Matthias Müller
  • 444
  • 6
  • 15
dsi
  • 3,199
  • 12
  • 59
  • 102
  • 1
    what have you tried? and are you *tied* to `XmlDocument`? in `XDocument`, `el.Attribute("xmlns")?.Remove();` seems to work... (obviously in real code you'd want to test the xmlns against the element's .Name.Namespace, and only remove if different) – Marc Gravell Jul 09 '19 at 16:10
  • don't have, linq.xml. its plain - xmldocument/xmlNode. – dsi Jul 09 '19 at 16:11
  • the link above is using with xElement from linq.xml.. here it does'nt have. – dsi Jul 09 '19 at 16:15
  • yeah, I'm undoing that close-as-duplicate; that's a completely different and incompatible API – Marc Gravell Jul 09 '19 at 16:16
  • 1
    Where are you getting this XML from? It looks very suspicious; colons are used to separate XML namespaces from tags, and the `xmlns` attribute is used to distinguish namespaces. But these are all wrong in your XML fragment, the `z` namespace is never defined and the `xmlns` attribute does not define a namespace. Either this XML does not use namespaces, is badly corrupted, or you're not showing us the actual XML. – Dour High Arch Jul 09 '19 at 16:25
  • Yes,its like this as received stream and loaded with. related namespace on document. – dsi Jul 09 '19 at 17:57

1 Answers1

2

I guess you'd want something like:

XmlDocument doc = new XmlDocument();
doc.Load("my.xml");
foreach(var node in doc.DocumentElement.ChildNodes)
{
    var el = node as XmlElement;
    if (el != null)
    {
        if (el.HasAttribute("xmlns"))
        {
            var ns = el.GetAttribute("xmlns");
            if (ns != null && ns != el.NamespaceURI)
            {
                el.RemoveAttribute("xmlns");
            }
        }
    }
}
doc.Save("new.xml");

Obviously you'd probably want to make it query, or issue an all-elements query, in complex scenarios.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Problem is, ` if (el.HasAttribute("xmlns"))` does not get true but, el.OuterXml has xmlns and namespaceURI exists. – dsi Jul 09 '19 at 16:22
  • @dsi I tested it... it worked fine here; if you're seeing it failing in a specific scenario, it would help to post that exact scenario with representative code and xml; if you're saying that it isn't applying to the document/root element - fine: just fix that! – Marc Gravell Jul 09 '19 at 16:24