Skip to content

Instantly share code, notes, and snippets.

@dalegaspi
Last active November 10, 2020 16:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dalegaspi/9ef8a241339c1de62413 to your computer and use it in GitHub Desktop.
Save dalegaspi/9ef8a241339c1de62413 to your computer and use it in GitHub Desktop.
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