[英]How do I get rid of empty namespaces “xmlns=”“” in subelements?
I have this 我有这个
XNamespace ns = "http://something0.com";
XNamespace xsi = "http://something1.com";
XNamespace schemaLocation = "http://something3.com";
XDocument doc2 = new XDocument(
new XElement(ns.GetName("Foo"),
new XAttribute(XNamespace.Xmlns + "xsi", xsi),
new XAttribute(xsi.GetName("schemaLocation"), schemaLocation),
new XElement("ReportHeader", GetSection()),
GetGroup()
)
);
It gives 它给
<?xml version="1.0" encoding="utf-8"?>
<Foo xmlns:xsi="http://something1.com"
xsi:schemaLocation="http://something3.com"
xmlns="http://something0.com">
<ReportHeader xmlns="">
...
</ReportHeader>
<Group xmlns="">
...
</Group>
</Foo>
But I wan't this result, how can it be done? 但是我不想要这个结果,怎么办呢? (Notice the
xmlns=""
is missing..) (注意缺少
xmlns=""
。)
<?xml version="1.0" encoding="utf-8"?>
<Foo xmlns:xsi="http://something1.com"
xsi:schemaLocation="http://something3.com"
xmlns="http://something0.com">
<ReportHeader>
...
</ReportHeader>
<Group>
...
</Group>
</Foo>
Your problem here is that you are setting the default namespace for the document to "http://something0.com", but then appending elements that are not in this namespace - they are in the empty namespace. 这里的问题是您将文档的默认名称空间设置为“ http://something0.com”,但是随后追加了不在该名称空间中的元素-它们位于空名称空间中。
Your document states it has a default namespace of xmlns="http://something0.com", but then you append elements which are in the empty namespace (because you didn't supply their namespace when you appended them) - so they are all getting explicitly marked with xmlns='' to show they are not in the default namespace of the document. 您的文档指出其默认名称空间为xmlns =“ http://something0.com”,但随后您会添加空名称空间中的元素(因为添加元素时未提供其名称空间),因此它们是所有文件都显式标记为xmlns =''以表明它们不在文档的默认名称空间中。
This means there are two solutions to getting rid of the xmlns="", but they have different meanings: 这意味着有两种解决方案来摆脱xmlns =“”,但是它们具有不同的含义:
1) If you mean you definitely want the xmlns="http://something0.com"
at the root element (specifying the default namespace for the document) - then to "disappear" the xmlns="" you need to you need to supply this namespace when creating the elements: 1)如果您是绝对要在根元素(指定文档的默认名称空间)中使用
xmlns="http://something0.com"
-那么要“消失” xmlns =“”,您需要在创建元素时提供以下名称空间:
// create a ReportHeader element in the namespace http://something0.com
new XElement(ns + "ReportHeader", GetSection())
2) If these elements are not meant to be in the namespace "http://something0.com", then you mustn't add it as the default at the top of the document (the xmlns="http://something0.com" bit on the root element). 2)如果这些元素不是要放在“ http://something0.com”命名空间中,则您不必将其作为默认值添加到文档顶部(xmlns =“ http:// something0。 com”在根元素上)。
XDocument doc2 = new XDocument(
new XElement("foo", // note - just the element name, rather s.GetName("Foo")
new XAttribute(XNamespace.Xmlns + "xsi", xsi),
The sample output you expect, suggests the former of these two choices. 您期望的样本输出建议这两个选择中的前一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.