0

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>
Damini Suthar
  • 1,470
  • 2
  • 14
  • 43
  • Please edit your question and add desired output. – Yitzhak Khabinsky Feb 23 '22 at 03:00
  • @YitzhakKhabinsky added output. – Damini Suthar Feb 23 '22 at 03:03
  • Are you open to use XSLT? – Yitzhak Khabinsky Feb 23 '22 at 03:05
  • If I can get the required output than it's okay to use. – Damini Suthar Feb 23 '22 at 03:06
  • 1
    Why do you want to remove XML namespaces? Your reference to "clean" XML (namespaces are not "unclean"), your confusion between namespaces and `schemaLocation` attributes, and your lack of any justifying motivation suggest that obliging your request may do more harm than good. XML namespaces play an important role in identifying XML components and should not be removed without understanding that role fully. – kjhughes Feb 23 '22 at 03:07
  • @kjhughes I agree, but I want to do some Integration and I can do that only by removing namespaces. – Damini Suthar Feb 23 '22 at 03:15
  • See duplicate links for how to do it all in XSLT as @YitzhakKhabinsky suggests, or how to remove namespaces and how to remove attributes (`schemaLocation`) in C# as the question is tagged. – kjhughes Feb 23 '22 at 03:23

0 Answers0