Remove multiple namespaces from XML with C#

200 Views Asked by At

My XML having multiple namespaces. I want clean XML just with tags only. Here is my XML:

    <BizMsg xmlns="urn:asx:xsd:xasx.802.001.04" xmlns:xsi_0="http://www.w3.org/2001/XMLSchema-instance" xsi_0:schemaLocation="urn:asx:xsd:xasx.802.001.04 ASX_AU_CHS_comm_802_001_04_xasx_802_001_04.xsd">
      <AppHdr xmlns="urn:iso:std:iso:20022:tech:xsd:head.001.001.02" xmlns:xsi_1="http://www.w3.org/2001/XMLSchema-instance" xsi_1:schemaLocation="urn:iso:std:iso:20022:tech:xsd:head.001.001.02 ASX_AU_CHS_comm_801_001_02_head_001_001_02.xsd">
            <From>
            </From>
       </AppHdr>
  </BizMsg>

I am able to remove xmlns with this code:

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

By applying this code I am getting schemaLocation in result, I want to get just tag only as result. Please advise.

    <BizMsg schemaLocation="urn:asx:xsd:xasx.802.001.04 ASX_AU_CHS_comm_802_001_04_xasx_802_001_04.xsd">
      <AppHdr schemaLocation="urn:iso:std:iso:20022:tech:xsd:head.001.001.02 ASX_AU_CHS_comm_801_001_02_head_001_001_02.xsd">
       <From>
       </From>
     </AppHdr>
   </BizMsg>

I want to get as result:

   <BizMsg>
      <AppHdr>
       <From>
       </From>
     </AppHdr>
   </BizMsg>
0

There are 0 best solutions below