简体   繁体   English

如何更改命名空间的前缀?

[英]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 .最好使用System.Xml.XmlDocument

If it's not possible with .Net, do you know what is it possible with?如果 .Net 不可能,你知道什么是可能的吗?

I'm a bit rusted with this API, but you should be able to use XDocument to replace the namespace:我对这个 API 有点生疏,但你应该能够使用XDocument来替换命名空间:

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.保存时, XDocument重新生成 XML,只要您保持相同的 uri,就应该应用新的前缀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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