Created
October 24, 2011 15:32
-
-
Save bernardobrezende/1309326 to your computer and use it in GitHub Desktop.
How to remove XML namespaces
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.Linq; | |
using System.Xml.Linq; | |
namespace Foo.Namespace | |
{ | |
public class XmlUtils | |
{ | |
public static string RemoveAllNamespaces(string xmlDocument) | |
{ | |
XElement xmlDocumentWithoutNs = RemoveAllNamespaces(XElement.Parse(xmlDocument)); | |
return xmlDocumentWithoutNs.ToString(); | |
} | |
public static XElement RemoveAllNamespaces(XElement xmlDocument) | |
{ | |
if (!xmlDocument.HasElements) | |
{ | |
XElement xElement = new XElement(xmlDocument.Name.LocalName); | |
xElement.Value = xmlDocument.Value; | |
return xElement; | |
} | |
return new XElement(xmlDocument.Name.LocalName, xmlDocument.Elements().Select(el => RemoveAllNamespaces(el))); | |
} | |
public static string ReplaceSpecialCharacters(string serializedXml) | |
{ | |
return serializedXml.Replace("<", "<").Replace(""", "\"").Replace(">", ">"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment