Reputation: 3359
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
Upvotes: 0
Views: 9949
Reputation: 1062770
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.
Upvotes: 2