Skip to content

Instantly share code, notes, and snippets.

@bernardobrezende
Created October 24, 2011 15:32
Show Gist options
  • Save bernardobrezende/1309326 to your computer and use it in GitHub Desktop.
Save bernardobrezende/1309326 to your computer and use it in GitHub Desktop.
How to remove XML namespaces
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("&lt;", "<").Replace("&quot;", "\"").Replace("&gt;", ">");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment