Last active
November 10, 2020 16:15
-
-
Save dalegaspi/9ef8a241339c1de62413 to your computer and use it in GitHub Desktop.
Sample code for StackOverflow answer here: http://stackoverflow.com/questions/987135/how-to-remove-all-namespaces-from-xml-with-c/7238007#7238007
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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