The most natural and easy way to implement it is via XSLT.
Please find below an example of it.
The XSLT is generic. It will remove (1) XML prolog declaration, and (2) any namespace(s) from any XML file.
Input XML
<?xml version="1.0"?>
<AMOUNT_MONEY xmlns="http://www.somewhere.com/ABC" version="5.252">
<CURRENT_ADDRESS occupancy_status="RENT" occupancy_description="">
<FORMER_ADDRESS street_address_1="1234 CIRCLE LN" county=""/>
</CURRENT_ADDRESS>
</AMOUNT_MONEY>
XSLT
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/|comment()|processing-instruction()">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
Output XML
<AMOUNT_MONEY version="5.252">
<CURRENT_ADDRESS occupancy_status="RENT" occupancy_description="">
<FORMER_ADDRESS street_address_1="1234 CIRCLE LN" county="" />
</CURRENT_ADDRESS>
</AMOUNT_MONEY>
c# to launch XSLT transformation
void Main()
{
const string SOURCEXMLFILE = @"e:\Temp\UniversalShipment.xml";
const string XSLTFILE = @"e:\Temp\RemoveNamespaces.xslt";
const string OUTPUTXMLFILE = @"e:\temp\UniversalShipment_output.xml";
try
{
XsltArgumentList xslArg = new XsltArgumentList();
using (XmlReader src = XmlReader.Create(SOURCEXMLFILE))
{
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(XSLTFILE, new XsltSettings(true, true), new XmlUrlResolver());
XmlWriterSettings settings = xslt.OutputSettings.Clone();
settings.IndentChars = "\t";
// to remove BOM
settings.Encoding = new UTF8Encoding(false);
using (XmlWriter result = XmlWriter.Create(OUTPUTXMLFILE, settings))
{
xslt.Transform(src, xslArg, result, new XmlUrlResolver());
result.Close();
}
}
Console.WriteLine("File '{0}' has been generated.", OUTPUTXMLFILE);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}