Skip to content

Instantly share code, notes, and snippets.

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:
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"">
<emp:name emp:favoritecolor=""blue"">John Smith</emp:name>
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.