How to remove all namespaces from XML with C#?

前端 未结 30 2144
悲哀的现实
悲哀的现实 2020-11-22 13:30

I am looking for the clean, elegant and smart solution to remove namespacees from all XML elements? How would function to do that look like?

Defined interface:

30条回答
  •  孤街浪徒
    2020-11-22 13:50

    Without recreating whole node hierarchy:

    private static void RemoveDefNamespace(XElement element)
    {
        var defNamespase = element.Attribute("xmlns");
        if (defNamespase != null)
            defNamespase.Remove();
    
        element.Name = element.Name.LocalName;
        foreach (var child in element.Elements())
        {
            RemoveDefNamespace(child);
        }
    }
    

提交回复
热议问题