public static string RemoveAllNamespaces(string xmlDocument) { XElement xmlDocumentWithoutNs = RemoveAllNamespaces(XElement.Parse(xmlDocument)); return xmlDocumentWithoutNs.ToString(); } private static XElement RemoveAllNamespaces(XElement xmlDocument) { if (!xmlDocument.HasElements) { XElement xElement = new XElement(xmlDocument.Name.LocalName); xElement.Value = xmlDocument.Value; foreach (XAttribute attribute in xmlDocument.Attributes()) xElement.Add(attribute); return xElement; } return new XElement(xmlDocument.Name.LocalName, xmlDocument.Elements().Select(el => RemoveAllNamespaces(el))); }
{System.Xml.XmlException: The prefix '' cannot be redefined from '' to 'http://sample.response.power.core.com' within the same start element tag. at System.Xml.XmlWellFormedWriter.PushNamespaceExplicit(String prefix, String ns) at System.Xml.XmlWellFormedWriter.WriteEndAttribute() at System.Xml.Linq.ElementWriter.WriteStartElement(XElement e) at System.Xml.Linq.ElementWriter.WriteElement(XElement e) at System.Xml.Linq.XElement.WriteTo(XmlWriter writer) at System.Xml.Linq.XNode.GetXmlString(SaveOptions o) at System.Xml.Linq.XNode.ToString()
static XElement stripNS(XElement root) { return new XElement( root.Name.LocalName, root.HasElements ? root.Elements().Select(el => stripNS(el)) : (object)root.Value ); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)