C# 如何更改名称空间的前缀?
如何改变这一点C# 如何更改名称空间的前缀?,c#,.net,xml,powershell,xml-parsing,C#,.net,Xml,Powershell,Xml Parsing,如何改变这一点 <d xmlns='uri' xmlns:a='uri'> <a:c/> <c a:atr=':a:b:c:d:e:f:'/> </d> 进入这个 <d xmlns='uri' xmlns:b='uri'> <b:c/> <c b:atr=':a:b:c:d:e:f:'/> </d> ? 有可能吗?最好使用System.Xml.XmlDocu
<d xmlns='uri' xmlns:a='uri'>
<a:c/>
<c a:atr=':a:b:c:d:e:f:'/>
</d>
进入这个
<d xmlns='uri' xmlns:b='uri'>
<b:c/>
<c b:atr=':a:b:c:d:e:f:'/>
</d>
?
有可能吗?最好使用System.Xml.XmlDocument
如果.Net无法实现此功能,您知道它可以实现什么功能吗?我对此API有点生疏,但您应该能够使用替换命名空间:
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");
保存时,
XDocument
会重新生成XML,只要您保持相同的uri,就应该应用新前缀。我对该API有点生疏,但您应该能够使用替换命名空间:
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");
保存时,
XDocument
将重新生成XML,只要您保持相同的uri,就应该应用新前缀。可能重复:可能重复:System.XML.XmlDocument是否可能?因为如果xmlns
与xmlns:a
相同,则每个没有前缀的节点都将以b
作为前缀(如果a
更改为b
)。是否可以使用System.Xml.XmlDocument?因为如果xmlns
与xmlns:a
相同,每个没有前缀的节点都将以b
作为前缀(如果a
更改为b
)。