[英]Force XDocument to not use namespace prefix if namespace is also defined as default
I have an xml file with default namespace specified with and without namespace prefix. 我有一个xml文件,默认命名空间使用和不使用名称空间前缀。 When I generate xml output I'm getting all xml elements prefixed. 当我生成xml输出时,我得到所有xml元素前缀。 Is there way to get rid of the prefixes since I'm using the default namespace? 有没有办法摆脱前缀,因为我使用默认命名空间?
class Program
{
static void Main(string[] args)
{
var xml =
"<root xmlns='default-namespace' xmlns:key='default-namespace'>" +
" <node1></node1>" +
" <node2></node2>" +
"</root>";
var document = XDocument.Parse(xml);
var output = document.ToString();
}
}
The output: 输出:
<key:root xmlns="default-namespace" xmlns:key="default-namespace">
<key:node1></key:node1>
<key:node2></key:node2>
</key:root>
What I'd expect: 我期待的是:
<root xmlns="default-namespace" xmlns:key="default-namespace">
<node1></node1>
<node2></node2>
</root>
Unfortunately I can't remove the duplicated namespace declaration. 不幸的是,我无法删除重复的名称空间声明。 The actual xml file I'm using is provided by another party and I need to do as less modifications as possible. 我正在使用的实际xml文件是由另一方提供的,我需要做尽可能少的修改。
You Can Use String.Replace Method to Get The Required XML Format ... 您可以使用String.Replace方法获取所需的XML格式...
var xml ="<root xmlns='default-namespace' xmlns:key='default-namespace'>" +
" <node1></node1>" +
" <node2></node2>" +
"</root>";
var document = XDocument.Parse(xml);
var output = document.ToString().Replace("key:", string.Empty);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.