Wie entferne ich alle Namespaces aus XML mit C #?

Ich suche nach der sauberen, eleganten und intelligenten Lösung, um Namensräume aus allen XML-Elementen zu entfernen? Wie würde das aussehen?

Definierte Schnittstelle:

public interface IXMLUtils { string RemoveAllNamespaces(string xmlDocument); } 

Beispiel-XML zum Entfernen von NS von:

    0174587 014717 019172    false  Some state   

Nachdem wir RemoveAllNamespaces (xmlWithLotOfNs) aufgerufen haben, erhalten wir:

     0174587 014717 019172    false  Some state   

Bevorzugte Sprache der Lösung ist C # auf .NET 3.5 SP1.

   

Nun, hier ist die endgültige Antwort. Ich habe große Jimmy-Idee (die leider nicht abgeschlossen ist) und vollständige Rekursionsfunktion verwendet, um ordnungsgemäß zu funktionieren.

Basierend auf Schnittstelle:

 string RemoveAllNamespaces(string xmlDocument); 

Ich stelle hier eine endgültige saubere und universelle C # -Lösung zum Entfernen von XML-Namespaces vor:

 //Implemented based on interface, not part of algorithm public static string RemoveAllNamespaces(string xmlDocument) { XElement xmlDocumentWithoutNs = RemoveAllNamespaces(XElement.Parse(xmlDocument)); return xmlDocumentWithoutNs.ToString(); } //Core recursion function 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))); } 

Es funktioniert 100%, aber ich habe es nicht viel getestet, so dass es nicht einige spezielle Fälle abdecken kann … Aber es ist eine gute Basis, um zu starten.

Die am meisten nützliche Antwort hat zwei Fehler:

  • Es ignoriert Attribute
  • Es funktioniert nicht mit “gemischten Modus” -Elementen

Hier ist meine Meinung zu diesem Thema:

  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); } 

Beispielcode hier .

die obligatorische Antwort mit LINQ:

 static XElement stripNS(XElement root) { return new XElement( root.Name.LocalName, root.HasElements ? root.Elements().Select(el => stripNS(el)) : (object)root.Value ); } static void Main() { var xml = XElement.Parse(@"< ?xml version=""1.0"" encoding=""utf-16""?>   0174587 014717 019172    
false
Some state
"); Console.WriteLine(stripNS(xml)); }

Das wird den Trick machen 🙂

 foreach (XElement XE in Xml.DescendantsAndSelf()) { // Stripping the namespace by setting the name of the element to it's localname only XE.Name = XE.Name.LocalName; // replacing all attributes with attributes that are not namespaces and their names are set to only the localname XE.ReplaceAttributes((from xattrib in XE.Attributes().Where(xa => !xa.IsNamespaceDeclaration) select new XAttribute(xattrib.Name.LocalName, xattrib.Value))); } 

Holen Sie es wieder auf, in C # – Zeile für das Kopieren der Attribute hinzugefügt:

  static XElement stripNS(XElement root) { XElement res = new XElement( root.Name.LocalName, root.HasElements ? root.Elements().Select(el => stripNS(el)) : (object)root.Value ); res.ReplaceAttributes( root.Attributes().Where(attr => (!attr.IsNamespaceDeclaration))); return res; } 

Die obligatorische Antwort mit XSLT:

                   

Ich weiß, dass diese Frage angeblich getriggers ist, aber ich war nicht ganz glücklich mit der Art, wie sie umgesetzt wurde. Ich habe hier in den MSDN-Blogs eine andere Quelle gefunden, die über eine überschriebene XmlTextWriter class verfügt, die die Namespaces XmlTextWriter . Ich habe es ein wenig optimiert, um einige andere Dinge zu bekommen, die ich wollte, wie z. B. eine hübsche Formatierung und das Bewahren des Wurzelelements. Hier ist, was ich gerade in meinem Projekt habe.

http://blogs.msdn.com/b/kaevans/archive/2004/08/02/206432.aspx

class

 ///  /// Modified XML writer that writes (almost) no namespaces out with pretty formatting ///  ///  public class XmlNoNamespaceWriter : XmlTextWriter { private bool _SkipAttribute = false; private int _EncounteredNamespaceCount = 0; public XmlNoNamespaceWriter(TextWriter writer) : base(writer) { this.Formatting = System.Xml.Formatting.Indented; } public override void WriteStartElement(string prefix, string localName, string ns) { base.WriteStartElement(null, localName, null); } public override void WriteStartAttribute(string prefix, string localName, string ns) { //If the prefix or localname are "xmlns", don't write it. //HOWEVER... if the 1st element (root?) has a namespace we will write it. if ((prefix.CompareTo("xmlns") == 0 || localName.CompareTo("xmlns") == 0) && _EncounteredNamespaceCount++ > 0) { _SkipAttribute = true; } else { base.WriteStartAttribute(null, localName, null); } } public override void WriteString(string text) { //If we are writing an attribute, the text for the xmlns //or xmlns:prefix declaration would occur here. Skip //it if this is the case. if (!_SkipAttribute) { base.WriteString(text); } } public override void WriteEndAttribute() { //If we skipped the WriteStartAttribute call, we have to //skip the WriteEndAttribute call as well or else the XmlWriter //will have an invalid state. if (!_SkipAttribute) { base.WriteEndAttribute(); } //reset the boolean for the next attribute. _SkipAttribute = false; } public override void WriteQualifiedName(string localName, string ns) { //Always write the qualified name using only the //localname. base.WriteQualifiedName(localName, null); } } 

Verwendung

 //Save the updated document using our modified (almost) no-namespace XML writer using(StreamWriter sw = new StreamWriter(this.XmlDocumentPath)) using(XmlNoNamespaceWriter xw = new XmlNoNamespaceWriter(sw)) { //This variable is of type `XmlDocument` this.XmlDocumentRoot.Save(xw); } 

Und das ist die perfekte Lösung, die auch XSI-Elemente entfernen wird. (Wenn Sie die xmlns entfernen und XSI nicht entfernen, ruft .Net Sie an …)

 string xml = node.OuterXml; //Regex below finds strings that start with xmlns, may or may not have :and some text, then continue with = //and ", have a streach of text that does not contain quotes and end with ". similar, will happen to an attribute // that starts with xsi. string strXMLPattern = @"xmlns(:\w+)?=""([^""]+)""|xsi(:\w+)?=""([^""]+)"""; xml = Regex.Replace(xml, strXMLPattern, ""); 

Dies ist eine Lösung, die auf Peter Stegnars akzeptierter Antwort basiert.

Ich habe es benutzt, aber (wie andygjp und John Saunders bemerkt haben) ignoriert sein Code Attribute .

Ich musste mich auch um Attribute kümmern, also passte ich seinen Code an. Andys Version war Visual Basic, das ist immer noch c #.

Ich weiß, dass es eine Weile her ist, aber vielleicht wird es eines Tages jemanden retten.

  private static XElement RemoveAllNamespaces(XElement xmlDocument) { XElement xmlDocumentWithoutNs = removeAllNamespaces(xmlDocument); return xmlDocumentWithoutNs; } private static XElement removeAllNamespaces(XElement xmlDocument) { var stripped = new XElement(xmlDocument.Name.LocalName); foreach (var attribute in xmlDocument.Attributes().Where( attribute => !attribute.IsNamespaceDeclaration && String.IsNullOrEmpty(attribute.Name.NamespaceName))) { stripped.Add(new XAttribute(attribute.Name.LocalName, attribute.Value)); } if (!xmlDocument.HasElements) { stripped.Value = xmlDocument.Value; return stripped; } stripped.Add(xmlDocument.Elements().Select( el => RemoveAllNamespaces(el))); return stripped; } 

Ich mag es sehr, wo Dexter dort hin geht, also übersetzte ich es in eine “fließende” Erweiterungsmethode:

 ///  /// Returns the specified  /// without namespace qualifiers on elements and attributes. ///  /// The element public static XElement WithoutNamespaces(this XElement element) { if (element == null) return null; #region delegates: Func getChildNode = e => (e.NodeType == XmlNodeType.Element) ? (e as XElement).WithoutNamespaces() : e; Func> getAttributes = e => (e.HasAttributes) ? e.Attributes() .Where(a => !a.IsNamespaceDeclaration) .Select(a => new XAttribute(a.Name.LocalName, a.Value)) : Enumerable.Empty(); #endregion return new XElement(element.Name.LocalName, element.Nodes().Select(getChildNode), getAttributes(element)); } 

Der “fließende” Ansatz ermöglicht mir dies:

 var xml = File.ReadAllText(presentationFile); var xDoc = XDocument.Parse(xml); var xRoot = xDoc.Root.WithoutNamespaces(); 

Leicht modifizierte Peters Antwort, das würde auch für das Attribut gut funktionieren, einschließlich des Namensraums und des Präfixes. Ein bisschen Entschuldigung für den Code sieht ein bisschen hässlich aus.

  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(new XAttribute(attribute.Name.LocalName, attribute.Value)); } return xElement; } else { XElement xElement = new XElement(xmlDocument.Name.LocalName, xmlDocument.Elements().Select(el => RemoveAllNamespaces(el))); foreach (XAttribute attribute in xmlDocument.Attributes()) { xElement.Add(new XAttribute(attribute.Name.LocalName, attribute.Value)); } return xElement; } } 

Sie können das mit Linq tun:

 public static string RemoveAllNamespaces(string xmlDocument) { var xml = XElement.Parse(xmlDocument); xml.Descendants().Select(o => o.Name = o.Name.LocalName).ToArray(); return xml.ToString(); } 

Die Antwort von Jimmy und Peter war eine große Hilfe, aber sie haben tatsächlich alle Attribute entfernt, also habe ich eine kleine Änderung vorgenommen:

 Imports System.Runtime.CompilerServices Friend Module XElementExtensions  _ Public Function RemoveAllNamespaces(ByVal element As XElement) As XElement If element.HasElements Then Dim cleanElement = RemoveAllNamespaces(New XElement(element.Name.LocalName, element.Attributes)) cleanElement.Add(element.Elements.Select(Function(el) RemoveAllNamespaces(el))) Return cleanElement Else Dim allAttributesExceptNamespaces = element.Attributes.Where(Function(attr) Not attr.IsNamespaceDeclaration) element.ReplaceAttributes(allAttributesExceptNamespaces) Return element End If End Function End Module 

Damit Attribute funktionieren, die for-Schleife zum Hinzufügen von Attribut sollte nach Rekursion gehen, müssen auch überprüfen, ob IsNamespaceDeclaration:

 private static XElement RemoveAllNamespaces(XElement xmlDocument) { XElement xElement; if (!xmlDocument.HasElements) { xElement = new XElement(xmlDocument.Name.LocalName) { Value = xmlDocument.Value }; } else { xElement = new XElement(xmlDocument.Name.LocalName, xmlDocument.Elements().Select(RemoveAllNamespaces)); } foreach (var attribute in xmlDocument.Attributes()) { if (!attribute.IsNamespaceDeclaration) { xElement.Add(attribute); } } return xElement; } 

Hier ist meine VB.NET Version von Dexter Legaspi C # Version

 Shared Function RemoveAllNamespaces(ByVal e As XElement) As XElement Return New XElement(e.Name.LocalName, New Object() {(From n In e.Nodes Select If(TypeOf n Is XElement, RemoveAllNamespaces(TryCast(n, XElement)), n)), If(e.HasAttributes, (From a In e.Attributes Select a), Nothing)}) End Function 

Eine andere Lösung, die möglicherweise verschachtelte TEXT- und ELEMENT-Knoten berücksichtigt, zB:

  text1  text2   

Code:

 using System.Linq; namespace System.Xml.Linq { public static class XElementTransformExtensions { public static XElement WithoutNamespaces(this XElement source) { return new XElement(source.Name.LocalName, source.Attributes().Select(WithoutNamespaces), source.Nodes().Select(WithoutNamespaces) ); } public static XAttribute WithoutNamespaces(this XAttribute source) { return !source.IsNamespaceDeclaration ? new XAttribute(source.Name.LocalName, source.Value) : default(XAttribute); } public static XNode WithoutNamespaces(this XNode source) { return source is XElement ? WithoutNamespaces((XElement)source) : source; } } } 

Ohne auf eine XSLT-basierte Lösung zurückgreifen zu müssen, wenn Sie sauber, elegant und intelligent sein möchten, benötigen Sie Unterstützung durch das Framework, insbesondere das Besuchermuster könnte dies zu einem Kinderspiel machen. Leider ist es hier nicht verfügbar.

Ich habe es inspiriert von LINQs ExpressionVisitor implementiert, um eine ähnliche Struktur zu haben. Damit können Sie das Besuchermuster auf (LINQ-to-) XML-Objekte anwenden. (Ich habe dies nur eingeschränkt getestet, aber es funktioniert soweit ich das beurteilen kann)

 public abstract class XObjectVisitor { public virtual XObject Visit(XObject node) { if (node != null) return node.Accept(this); return node; } public ReadOnlyCollection Visit(IEnumerable nodes) { return nodes.Select(node => Visit(node)) .Where(node => node != null) .ToList() .AsReadOnly(); } public T VisitAndConvert(T node) where T : XObject { if (node != null) return Visit(node) as T; return node; } public ReadOnlyCollection VisitAndConvert(IEnumerable nodes) where T : XObject { return nodes.Select(node => VisitAndConvert(node)) .Where(node => node != null) .ToList() .AsReadOnly(); } protected virtual XObject VisitAttribute(XAttribute node) { return node.Update(node.Name, node.Value); } protected virtual XObject VisitComment(XComment node) { return node.Update(node.Value); } protected virtual XObject VisitDocument(XDocument node) { return node.Update( node.Declaration, VisitAndConvert(node.Nodes()) ); } protected virtual XObject VisitElement(XElement node) { return node.Update( node.Name, VisitAndConvert(node.Attributes()), VisitAndConvert(node.Nodes()) ); } protected virtual XObject VisitDocumentType(XDocumentType node) { return node.Update( node.Name, node.PublicId, node.SystemId, node.InternalSubset ); } protected virtual XObject VisitProcessingInstruction(XProcessingInstruction node) { return node.Update( node.Target, node.Data ); } protected virtual XObject VisitText(XText node) { return node.Update(node.Value); } protected virtual XObject VisitCData(XCData node) { return node.Update(node.Value); } #region Implementation details internal InternalAccessor Accessor { get { return new InternalAccessor(this); } } internal class InternalAccessor { private XObjectVisitor visitor; internal InternalAccessor(XObjectVisitor visitor) { this.visitor = visitor; } internal XObject VisitAttribute(XAttribute node) { return visitor.VisitAttribute(node); } internal XObject VisitComment(XComment node) { return visitor.VisitComment(node); } internal XObject VisitDocument(XDocument node) { return visitor.VisitDocument(node); } internal XObject VisitElement(XElement node) { return visitor.VisitElement(node); } internal XObject VisitDocumentType(XDocumentType node) { return visitor.VisitDocumentType(node); } internal XObject VisitProcessingInstruction(XProcessingInstruction node) { return visitor.VisitProcessingInstruction(node); } internal XObject VisitText(XText node) { return visitor.VisitText(node); } internal XObject VisitCData(XCData node) { return visitor.VisitCData(node); } } #endregion } public static class XObjectVisitorExtensions { #region XObject.Accept "instance" method public static XObject Accept(this XObject node, XObjectVisitor visitor) { Validation.CheckNullReference(node); Validation.CheckArgumentNull(visitor, "visitor"); // yay, easy dynamic dispatch Acceptor acceptor = new Acceptor(node as dynamic); return acceptor.Accept(visitor); } private class Acceptor { public Acceptor(XAttribute node) : this(v => v.Accessor.VisitAttribute(node)) { } public Acceptor(XComment node) : this(v => v.Accessor.VisitComment(node)) { } public Acceptor(XDocument node) : this(v => v.Accessor.VisitDocument(node)) { } public Acceptor(XElement node) : this(v => v.Accessor.VisitElement(node)) { } public Acceptor(XDocumentType node) : this(v => v.Accessor.VisitDocumentType(node)) { } public Acceptor(XProcessingInstruction node) : this(v => v.Accessor.VisitProcessingInstruction(node)) { } public Acceptor(XText node) : this(v => v.Accessor.VisitText(node)) { } public Acceptor(XCData node) : this(v => v.Accessor.VisitCData(node)) { } private Func accept; private Acceptor(Func accept) { this.accept = accept; } public XObject Accept(XObjectVisitor visitor) { return accept(visitor); } } #endregion #region XObject.Update "instance" method public static XObject Update(this XAttribute node, XName name, string value) { Validation.CheckNullReference(node); Validation.CheckArgumentNull(name, "name"); Validation.CheckArgumentNull(value, "value"); return new XAttribute(name, value); } public static XObject Update(this XComment node, string value = null) { Validation.CheckNullReference(node); return new XComment(value); } public static XObject Update(this XDocument node, XDeclaration declaration = null, params object[] content) { Validation.CheckNullReference(node); return new XDocument(declaration, content); } public static XObject Update(this XElement node, XName name, params object[] content) { Validation.CheckNullReference(node); Validation.CheckArgumentNull(name, "name"); return new XElement(name, content); } public static XObject Update(this XDocumentType node, string name, string publicId = null, string systemId = null, string internalSubset = null) { Validation.CheckNullReference(node); Validation.CheckArgumentNull(name, "name"); return new XDocumentType(name, publicId, systemId, internalSubset); } public static XObject Update(this XProcessingInstruction node, string target, string data) { Validation.CheckNullReference(node); Validation.CheckArgumentNull(target, "target"); Validation.CheckArgumentNull(data, "data"); return new XProcessingInstruction(target, data); } public static XObject Update(this XText node, string value = null) { Validation.CheckNullReference(node); return new XText(value); } public static XObject Update(this XCData node, string value = null) { Validation.CheckNullReference(node); return new XCData(value); } #endregion } public static class Validation { public static void CheckNullReference(T obj) where T : class { if (obj == null) throw new NullReferenceException(); } public static void CheckArgumentNull(T obj, string paramName) where T : class { if (obj == null) throw new ArgumentNullException(paramName); } } 

ps, diese spezielle Implementierung verwendet einige .NET 4-functionen, um die Implementierung ein wenig einfacher / sauberer zu machen (Verwendung von dynamic und Standardargumenten). Es sollte nicht allzu schwierig sein, .NET 3.5 kompatibel, vielleicht sogar .NET 2.0 kompatibel zu machen.

Um den Besucher zu implementieren, gibt es hier einen generalisierten, der mehrere Namespaces (und das verwendete Präfix) ändern kann.

 public class ChangeNamespaceVisitor : XObjectVisitor { private INamespaceMappingManager manager; public ChangeNamespaceVisitor(INamespaceMappingManager manager) { Validation.CheckArgumentNull(manager, "manager"); this.manager = manager; } protected INamespaceMappingManager Manager { get { return manager; } } private XName ChangeNamespace(XName name) { var mapping = Manager.GetMapping(name.Namespace); return mapping.ChangeNamespace(name); } private XObject ChangeNamespaceDeclaration(XAttribute node) { var mapping = Manager.GetMapping(node.Value); return mapping.ChangeNamespaceDeclaration(node); } protected override XObject VisitAttribute(XAttribute node) { if (node.IsNamespaceDeclaration) return ChangeNamespaceDeclaration(node); return node.Update(ChangeNamespace(node.Name), node.Value); } protected override XObject VisitElement(XElement node) { return node.Update( ChangeNamespace(node.Name), VisitAndConvert(node.Attributes()), VisitAndConvert(node.Nodes()) ); } } // and all the gory implementation details public class NamespaceMappingManager : INamespaceMappingManager { private Dictionary namespaces = new Dictionary(); public NamespaceMappingManager Add(XNamespace fromNs, XNamespace toNs, string toPrefix = null) { var item = new NamespaceMapping(fromNs, toNs, toPrefix); namespaces.Add(item.FromNs, item); return this; } public INamespaceMapping GetMapping(XNamespace fromNs) { INamespaceMapping mapping; if (!namespaces.TryGetValue(fromNs, out mapping)) mapping = new NullMapping(); return mapping; } private class NullMapping : INamespaceMapping { public XName ChangeNamespace(XName name) { return name; } public XObject ChangeNamespaceDeclaration(XAttribute node) { return node.Update(node.Name, node.Value); } } private class NamespaceMapping : INamespaceMapping { private XNamespace fromNs; private XNamespace toNs; private string toPrefix; public NamespaceMapping(XNamespace fromNs, XNamespace toNs, string toPrefix = null) { this.fromNs = fromNs ?? ""; this.toNs = toNs ?? ""; this.toPrefix = toPrefix; } public XNamespace FromNs { get { return fromNs; } } public XNamespace ToNs { get { return toNs; } } public string ToPrefix { get { return toPrefix; } } public XName ChangeNamespace(XName name) { return name.Namespace == fromNs ? toNs + name.LocalName : name; } public XObject ChangeNamespaceDeclaration(XAttribute node) { if (node.Value == fromNs.NamespaceName) { if (toNs == XNamespace.None) return null; var xmlns = !String.IsNullOrWhiteSpace(toPrefix) ? (XNamespace.Xmlns + toPrefix) : node.Name; return node.Update(xmlns, toNs.NamespaceName); } return node.Update(node.Name, node.Value); } } } public interface INamespaceMappingManager { INamespaceMapping GetMapping(XNamespace fromNs); } public interface INamespaceMapping { XName ChangeNamespace(XName name); XObject ChangeNamespaceDeclaration(XAttribute node); } 

Und eine kleine Helfermethode, um den Ball ins Rollen zu bringen:

 T ChangeNamespace(T node, XNamespace fromNs, XNamespace toNs, string toPrefix = null) where T : XObject { return node.Accept( new ChangeNamespaceVisitor( new NamespaceMappingManager() .Add(fromNs, toNs, toPrefix) ) ) as T; } 

Um dann einen Namespace zu entfernen, könnte man es so nennen:

 var doc = ChangeNamespace(XDocument.Load(pathToXml), fromNs: "http://schema.peters.com/doc_353/1/Types", toNs: null); 

Mit diesem Besucher können Sie einen INamespaceMappingManager schreiben, um alle Namespaces zu entfernen.

 T RemoveAllNamespaces(T node) where T : XObject { return node.Accept( new ChangeNamespaceVisitor(new RemoveNamespaceMappingManager()) ) as T; } public class RemoveNamespaceMappingManager : INamespaceMappingManager { public INamespaceMapping GetMapping(XNamespace fromNs) { return new RemoveNamespaceMapping(); } private class RemoveNamespaceMapping : INamespaceMapping { public XName ChangeNamespace(XName name) { return name.LocalName; } public XObject ChangeNamespaceDeclaration(XAttribute node) { return null; } } } 

Einfache Lösung, die die Elemente tatsächlich umbenennt, keine Kopie erstellt und die Attribute ziemlich gut ersetzt.

 public void RemoveAllNamespaces(ref XElement value) { List attributesToRemove = new List(); foreach (void e_loopVariable in value.DescendantsAndSelf) { e = e_loopVariable; if (e.Name.Namespace != XNamespace.None) { e.Name = e.Name.LocalName; } foreach (void a_loopVariable in e.Attributes) { a = a_loopVariable; if (a.IsNamespaceDeclaration) { //do not keep it at all attributesToRemove.Add(a); } else if (a.Name.Namespace != XNamespace.None) { e.SetAttributeValue(a.Name.LocalName, a.Value); attributesToRemove.Add(a); } } } foreach (void a_loopVariable in attributesToRemove) { a = a_loopVariable; a.Remove(); } } 

Hinweis: Dies behält nicht immer die ursprüngliche Attributreihenfolge bei, aber ich bin mir sicher, dass Sie es ändern können, um das ziemlich einfach zu tun, wenn es Ihnen wichtig ist.

Beachten Sie, dass dies auch eine Ausnahme auslösen könnte , wenn Sie über XElement-Attribute verfügen, die nur für den Namespace eindeutig sind:

    

Das scheint wirklich ein inhärentes Problem zu sein. But since the question indicated outputing a String, not an XElement, in this case you could have a solution that would output a valid String that was an invalid XElement.

I also liked jocull’s answer using a custom XmlWriter, but when I tried it, it did not work for me. Although it all looks correct, I couldn’t tell if the XmlNoNamespaceWriter class had any effect at all; it definitely was not removing the namespaces as I wanted it to.

Adding my that also cleans out the name of nodes that have namespace prefixes:

  public static string RemoveAllNamespaces(XElement element) { string tex = element.ToString(); var nsitems = element.DescendantsAndSelf().Select(n => n.ToString().Split(' ', '>')[0].Split('< ')[1]).Where(n => n.Contains(":")).DistinctBy(n => n).ToArray(); //Namespace prefix on nodes:  tex = nsitems.Aggregate(tex, (current, nsnode) => current.Replace("< "+nsnode + "", "<" + nsnode.Split(':')[1] + "")); tex = nsitems.Aggregate(tex, (current, nsnode) => current.Replace(" d.Attributes().Where(a => a.IsNamespaceDeclaration || a.ToString().Contains(":"))).DistinctBy(o => o.Value); tex = items.Aggregate(tex, (current, xAttribute) => current.Replace(xAttribute.ToString(), "")); return tex; } 

I tried the first few solutions and didn’t work for me. Mainly the problem with attributes being removed like the other have already mentioned. I would say my approach is very similar to Jimmy by using the XElement constructors that taking object as parameters.

 public static XElement RemoveAllNamespaces(this XElement element) { return new XElement(element.Name.LocalName, element.HasAttributes ? element.Attributes().Select(a => new XAttribute(a.Name.LocalName, a.Value)) : null, element.HasElements ? element.Elements().Select(e => RemoveAllNamespaces(e)) : null, element.Value); } 

my answer, string-manipulation-based,
lite-most code,

 public static string hilangkanNamespace(string instrXML) { char chrOpeningTag = '< '; char chrClosingTag = '>'; char chrSpasi = ' '; int intStartIndex = 0; do { int intIndexKu = instrXML.IndexOf(chrOpeningTag, intStartIndex); if (intIndexKu < 0) break; //kalau dah ga ketemu keluar int intStart = instrXML.IndexOfAny(new char[] { chrSpasi, chrClosingTag }, intIndexKu + 1); //mana yang ketemu duluan if (intStart < 0) break; //kalau dah ga ketemu keluar int intStop = instrXML.IndexOf(chrClosingTag, intStart); if (intStop < 0) break; //kalau dah ga ketemu keluar else intStop--; //exclude si closingTag int intLengthToStrip = intStop - intStart + 1; instrXML = instrXML.Remove(intStart, intLengthToStrip); intStartIndex = intStart; } while (true); return instrXML; } 

user892217’s answer is almost correct. It won’t compile as is, so needs a slight correction to the recursive call:

 private static XElement RemoveAllNamespaces(XElement xmlDocument) { XElement xElement; if (!xmlDocument.HasElements) { xElement = new XElement(xmlDocument.Name.LocalName) { Value = xmlDocument.Value }; } else { xElement = new XElement(xmlDocument.Name.LocalName, xmlDocument.Elements().Select(x => RemoveAllNamespaces(x))); } foreach (var attribute in xmlDocument.Attributes()) { if (!attribute.IsNamespaceDeclaration) { xElement.Add(attribute); } } return xElement; } 

Das hat für mich funktioniert.

  FileStream fs = new FileStream(filePath, FileMode.Open); StreamReader sr = new StreamReader(fs); DataSet ds = new DataSet(); ds.ReadXml(sr); ds.Namespace = ""; string outXML = ds.GetXml(); ds.Dispose(); sr.Dispose(); fs.Dispose(); 

After much searching for a solution to this very issue, this particular page seemed to have the most beef…however, nothing quite fit exactly, so I took the old-fashioned way and just parsed the stuff out I wanted out. Hoffe, das hilft jemandem. (Note: this also removes the SOAP or similar envelope stuff.)

  public static string RemoveNamespaces(string psXml) { // // parse through the passed XML, and remove any and all namespace references...also // removes soap envelope/header(s)/body, or any other references via ":" entities, // leaving all data intact // string xsXml = "", xsCurrQtChr = ""; int xiPos = 0, xiLastPos = psXml.Length - 1; bool xbInNode = false; while (xiPos < = xiLastPos) { string xsCurrChr = psXml.Substring(xiPos, 1); xiPos++; if (xbInNode) { if (xsCurrChr == ":") { // soap envelope or body (or some such) // we'll strip these node wrappers completely // need to first strip the beginning of it off (ie "") xsChr = "x"; // stay in loop...this is not end of node } } } } while (xsChr != ">" && xiPos < = xiLastPos); xiPos++; // skip over closing ">" xbInNode = false; } else { if (xsCurrChr == ">") { xbInNode = false; xsXml += xsCurrChr; } else { if (xsCurrChr == " " || xsCurrChr == "\t") { // potential namespace...let's check...next character must be "/" // or more white space, and if not, skip until we find such string xsChr = ""; int xiOrgLen = xsXml.Length; xsXml += xsCurrChr; do { if (xiPos < = xiLastPos) { xsChr = psXml.Substring(xiPos, 1); xiPos++; if (xsChr == " " || xsChr == "\r" || xsChr == "\n" || xsChr == "\t") { // carry on..white space xsXml += xsChr; } else { if (xsChr == "/" || xsChr == ">") { xsXml += xsChr; } else { // namespace! - get rid of it xsXml = xsXml.Substring(0, xiOrgLen - 0); // first, truncate any added whitespace // next, peek forward until we find "/" or ">" string xsQt = ""; do { if (xiPos < = xiLastPos) { xsChr = psXml.Substring(xiPos, 1); xiPos++; if (xsQt.Length > 0) { if (xsChr == xsQt) xsQt = ""; else xsChr = "x"; } else { if (xsChr == "'" || xsChr == "\"") xsQt = xsChr; } } } while (xsChr != ">" && xsChr != "/" && xiPos < = xiLastPos); if (xsChr == ">" || xsChr == "/") xsXml += xsChr; xbInNode = false; } } } } while (xsChr != ">" && xsChr != "/" && xiPos < = xiLastPos); } else { xsXml += xsCurrChr; } } } } else { // // if not currently inside a node, then we are in a value (or about to enter a new node) // xsXml += xsCurrChr; if (xsCurrQtChr.Length == 0) { if (xsCurrChr == "<") { xbInNode = true; } } else { // // currently inside a quoted string // if (xsCurrQtChr == xsCurrChr) { // finishing quoted string xsCurrQtChr = ""; } } } } return (xsXml); } 

Here’s are Regex Replace one liner:

 public static string RemoveNamespaces(this string xml) { return Regex.Replace(xml, "((?< =<|<\\/)|(?<= ))[A-Za-z0-9]+:| xmlns(:[A-Za-z0-9]+)?=\".*?\"", ""); } 

Here's a sample: https://regex101.com/r/fopydN/6

Warning:there might be edge cases!

Without recreating whole node hierarchy:

 private static void RemoveDefNamespace(XElement element) { var defNamespase = element.Attribute("xmlns"); if (defNamespase != null) defNamespase.Remove(); element.Name = element.Name.LocalName; foreach (var child in element.Elements()) { RemoveDefNamespace(child); } } 

Bit late to the party on this one but here’s what I used recently:

 var doc = XDocument.Parse(xmlString); doc.Root.DescendantNodesAndSelf().OfType().Attributes().Where(att => att.IsNamespaceDeclaration).Remove(); 

(taken from this MSDN Thread )

Edit As per the comment below, it appears that while this removes the namespace prefix from the nodes it doesn’t actually remove the xmlns attribute. To do that you need to also reset the name of each node to it’s localname (eg name minus namespace)

 foreach (var node in doc.Root.DescendantNodesAndSelf().OfType()) { node.Name = node.Name.LocalName; } 

Here is a regex based solution to this problem…

  private XmlDocument RemoveNS(XmlDocument doc) { var xml = doc.OuterXml; var newxml = Regex.Replace(xml, @"xmlns[:xsi|:xsd]*="".*?""",""); var newdoc = new XmlDocument(); newdoc.LoadXml(newxml); return newdoc; } 

I think this is shortest answer(but for constuctions like , you will have another discussion, I also have regex to convert "" to ” ” but it wasn’t optimized, If someone ask me I will share it. So, my solution is:

  public string RemoveAllNamespaces(string xmlDocument) { return Regex.Replace(xmlDocument, @"\sxmlns(\u003A\w+)?\u003D\u0022.+\u0022", " "); }