Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Threading.Tasks;
namespace RemoveXmlNamespaceApp
{
class Program
{
// my SO answer here: http://stackoverflow.com/questions/987135/how-to-remove-all-namespaces-from-xml-with-c/7238007#7238007
public static XElement RemoveAllNamespaces(XElement e)
{
return new XElement(e.Name.LocalName,
(from n in e.Nodes()
select ((n is XElement) ? RemoveAllNamespaces(n as XElement) : n)),
(e.HasAttributes) ? (from a in e.Attributes()
where (!a.IsNamespaceDeclaration)
select new XAttribute(a.Name.LocalName, a.Value)) : null);
}
static void Main(string[] args)
{
var doc = XElement.Parse(@"<list:employeeList xmlns:list=""urn:corp:list"" xmlns:emp=""urn:corp:emp"" xmlns:sec=""urn:corp:sec"">
<list:personList>
<emp:empID>E0000001</emp:empID>
<sec:name>Sales</sec:name>
<emp:name emp:favoritecolor=""blue"">John Smith</emp:name>
</list:personList>
<list:mixedmode>hello<emp:name>Smith</emp:name></list:mixedmode>
</list:employeeList>");
var new_doc = RemoveAllNamespaces(doc);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.