locked
How Do I Remove a Namespace from an XElement? RRS feed

  • Question

  • Dim root as XElement =

       <Root xmlns="http://services.pivot.rrd.com/US01_PVT9019WS/">
            <Child1>1</Child1>
            <Child2>2</Child2>
            <Child3>3</Child3>
        </Root>

    How do I remove the namespace?


    John Bailo RR Donnelley

    Sunday, March 30, 2014 10:56 PM

Answers

  • Hello,

    There is not implemented method to remove the XML namespace directly, we need to use way as custom method to loop the XML node and remove its namespaces as:

    static void Main(string[] args)
    
            {
    
                #region http://social.msdn.microsoft.com/Forums/en-US/9e6f77ad-9a7b-46c5-97ed-6ce9b5954e79/how-do-i-remove-a-namespace-from-an-xelement?forum=xmlandnetfx
    
                string xmlString = "<Root xmlns=\"http://services.pivot.rrd.com/US01_PVT9019WS/\"><Child1>1</Child1><Child2>2</Child2><Child3>3</Child3></Root>";
    
    
                XElement xmlDocumentWithoutNs = RemoveAllNamespaces(XElement.Parse(xmlString));
    
    
                #endregion
    
            }
    
    
            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)));
    
            }
    


    Copied from here:

    http://stackoverflow.com/questions/987135/how-to-remove-all-namespaces-from-xml-with-c

    The result is the original xml without the namespace.

    Although it uses the C#, the idea is there.

    If this does not work for you, plese let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, March 31, 2014 2:46 AM
    Moderator
  • Hope this helps.. 

     Dim root As XElement = XElement.Parse("<Root xmlns='http://services.pivot.rrd.com/US01_PVT9019WS/'><Child1>1</Child1><Child2>2</Child2><Child3>3</Child3></Root>")
            root.DescendantsAndSelf().Attributes().Where(Function(x) x.IsNamespaceDeclaration).Remove()
            For Each el As XElement In root.DescendantsAndSelf()
                el.Name = el.Name.LocalName
            Next
            Console.WriteLine(root)

    Monday, March 31, 2014 3:08 PM

All replies

  • Hello,

    There is not implemented method to remove the XML namespace directly, we need to use way as custom method to loop the XML node and remove its namespaces as:

    static void Main(string[] args)
    
            {
    
                #region http://social.msdn.microsoft.com/Forums/en-US/9e6f77ad-9a7b-46c5-97ed-6ce9b5954e79/how-do-i-remove-a-namespace-from-an-xelement?forum=xmlandnetfx
    
                string xmlString = "<Root xmlns=\"http://services.pivot.rrd.com/US01_PVT9019WS/\"><Child1>1</Child1><Child2>2</Child2><Child3>3</Child3></Root>";
    
    
                XElement xmlDocumentWithoutNs = RemoveAllNamespaces(XElement.Parse(xmlString));
    
    
                #endregion
    
            }
    
    
            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)));
    
            }
    


    Copied from here:

    http://stackoverflow.com/questions/987135/how-to-remove-all-namespaces-from-xml-with-c

    The result is the original xml without the namespace.

    Although it uses the C#, the idea is there.

    If this does not work for you, plese let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, March 31, 2014 2:46 AM
    Moderator
  • Hope this helps.. 

     Dim root As XElement = XElement.Parse("<Root xmlns='http://services.pivot.rrd.com/US01_PVT9019WS/'><Child1>1</Child1><Child2>2</Child2><Child3>3</Child3></Root>")
            root.DescendantsAndSelf().Attributes().Where(Function(x) x.IsNamespaceDeclaration).Remove()
            For Each el As XElement In root.DescendantsAndSelf()
                el.Name = el.Name.LocalName
            Next
            Console.WriteLine(root)

    Monday, March 31, 2014 3:08 PM
  • This method is buggy. For example, if my xml contains someting like this :

    <object objectNo="28" objectUid="abcdefg">
        <objectName>xx-111-xx</objectName>
    </object>

    this will produce as output 

    <object>
        <objectName>xx-111-xx</objectName>
    </object>
    We loose the attributes of the root object... 


    Benoit Potty

    Tuesday, May 15, 2018 9:26 AM
  • This done the trick for me : 

     private static void RemoveNamespaces(XElement xmlElement)
     {
        xmlElement.Name = xmlElement.Name.LocalName;
        xmlElement.ReplaceAttributes(
            from xattrib in xmlElement.Attributes()
                .Where(xa => !xa.IsNamespaceDeclaration)
                select new XAttribute(xattrib.Name.LocalName, xattrib.Value)
            );
    
         foreach (var descendant in xmlElement.Descendants())
         {
             RemoveNamespaces(descendant);
         }
    }

    And I use XElement.Parse to load muy received XML String.

    Hope this helps


    Benoit Potty

    • Proposed as answer by Benoit Potty Tuesday, May 15, 2018 11:42 AM
    Tuesday, May 15, 2018 11:42 AM