1

How to transform this

<d xmlns='uri' xmlns:a='uri'>
    <a:c/>
    <c a:atr=':a:b:c:d:e:f:'/>
</d>

into this

<d xmlns='uri' xmlns:b='uri'>
    <b:c/>
    <c b:atr=':a:b:c:d:e:f:'/>
</d>

?

Is it even possible? Preferably with System.Xml.XmlDocument.

If it's not possible with .Net, do you know what is it possible with?

Hrisip
  • 900
  • 4
  • 13
  • Possible duplicate: [xml-serialization-and-namespace-prefixes](https://stackoverflow.com/questions/2339782/xml-serialization-and-namespace-prefixes) – Xerillio Oct 10 '20 at 10:28

1 Answers1

0

I'm a bit rusted with this API, but you should be able to use XDocument to replace the namespace:

var document = XDocument.Load(@"E:\input.xml");

// Find and remove the namespace
document.Root.Attribute(XNamespace.Xmlns + "a").Remove();

// Add a new namespace with same uri and different prefix
document.Root.Add(new XAttribute(XNamespace.Xmlns + "b", "uri"));

document.Save(@"E:\output.xml");

When saving, XDocument regenerates the XML and should apply the new prefix as long as you keep the same uri.

Kevin Gosse
  • 38,392
  • 3
  • 78
  • 94