Remove Unused Namespaces from an Xml

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
I need to develop an C# code that removes all the unused namespaces from an System.Xml.XmlDocument, and promotes the only one used to default. That code must run on .Net 2.0.
I have inputs like this:

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <<span style="color:#A31515; ns0<span style="color:Blue; :<span style="color:#A31515; enviNFe <span style="color:Red; xmlns:ns1<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; http://www.w3.org/2000/09/xmldsig#<span style="color:Black; " <span style="color:Red; xmlns:ns0<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; http://www.portalfiscal.inf.br/nfe<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; ns0<span style="color:Blue; :<span style="color:#A31515; NFe<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; ns0<span style="color:Blue; :<span style="color:#A31515; infNFe<span style="color:Blue; >
...
<span style="color:Blue; </<span style="color:#A31515; ns0<span style="color:Blue; :<span style="color:#A31515; infNFe<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; ns0<span style="color:Blue; :<span style="color:#A31515; NFe<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; ns0<span style="color:Blue; :<span style="color:#A31515; enviNFe<span style="color:Blue; >
[/code]

and I need outputs like this:

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <<span style="color:#A31515; enviNFe <span style="color:Red; xmlns<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; http://www.portalfiscal.inf.br/nfe<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; NFe<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; infNFe<span style="color:Blue; >
...
<span style="color:Blue; </<span style="color:#A31515; infNFe<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; NFe<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; enviNFe<span style="color:Blue; >
[/code]
My inputs always have exactly one used namespace.

If possible, I want a solution that doesnt consist on string find / replace.

View the full article
 
Back
Top