简体   繁体   中英

How to change prefix of a namespace?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM