Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all namespaces from XML with C#?

Tags:

c#

xml

I am looking for the clean, elegant and smart solution to remove namespacees from all XML elements? How would function to do that look like?

Defined interface:

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

Sample XML to remove NS from:

<?xml version="1.0" encoding="utf-16"?> <ArrayOfInserts xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">   <insert>     <offer xmlns="http://schema.peters.com/doc_353/1/Types">0174587</offer>     <type2 xmlns="http://schema.peters.com/doc_353/1/Types">014717</type2>     <supplier xmlns="http://schema.peters.com/doc_353/1/Types">019172</supplier>     <id_frame xmlns="http://schema.peters.com/doc_353/1/Types" />     <type3 xmlns="http://schema.peters.com/doc_353/1/Types">       <type2 />       <main>false</main>     </type3>     <status xmlns="http://schema.peters.com/doc_353/1/Types">Some state</status>   </insert> </ArrayOfInserts> 

After we call RemoveAllNamespaces(xmlWithLotOfNs), we should get:

  <?xml version="1.0" encoding="utf-16"?>     <ArrayOfInserts>       <insert>         <offer >0174587</offer>         <type2 >014717</type2>         <supplier >019172</supplier>         <id_frame  />         <type3 >           <type2 />           <main>false</main>         </type3>         <status >Some state</status>       </insert>     </ArrayOfInserts> 

Preffered language of solution is C# on .NET 3.5 SP1.

like image 674
Peter Stegnar Avatar asked Jun 12 '09 15:06

Peter Stegnar


People also ask

How do I get rid of namespaces?

Remove namespaces using the Namespace Editor Determine the namespace for a node by selecting any object/node that uses the namespace. In the Namespace Editor, select the namespace you want to remove. Click Delete.

How do I remove namespace prefix?

Once a namespace prefix is created, it cannot be changed or deleted. The workaround is to move all your code to a new Developer Organization, where you can setup the desired Namespace Prefix.

How do I change the default namespace in XML?

You can change the default namespace within a particular element by adding an xmlns attribute to the element. Example 4-4 is an XML document that initially sets the default namespace to http://www.w3.org/1999/xhtml for all the XHTML elements.

Can XML have multiple namespaces?

When you use multiple namespaces in an XML document, you can define one namespace as the default namespace to create a cleaner looking document. The default namespace is declared in the root element and applies to all unqualified elements in the document. Default namespaces apply to elements only, not to attributes.


1 Answers

Well, here is the final answer. I have used great Jimmy idea (which unfortunately is not complete itself) and complete recursion function to work properly.

Based on interface:

string RemoveAllNamespaces(string xmlDocument); 

I represent here final clean and universal C# solution for removing XML namespaces:

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

It's working 100%, but I have not tested it much so it may not cover some special cases... But it is good base to start.

like image 96
Peter Stegnar Avatar answered Sep 28 '22 17:09

Peter Stegnar