在 LINQ 中仅设置一个命名空间
我在 LINQ 中的命名空间方面遇到一些问题。因此,从 MSDN 教程开始,我们可以执行以下操作来将命名空间添加到一个带有 LINQ 的 XML 文档:
// Create an XML tree in a namespace.
XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "Root",
new XElement(aw + "Child", "child content")
);
Console.WriteLine(root);
这会给你 XML:
<Root xmlns="http://www.adventure-works.com">
<Child>child content</Child>
</Root>
但我想做的是这样的:
XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "Roots");
XElement child = new XElement("Child", "child content");
root.Add(child);
Console.WriteLine(root);
但这给了我以下 xml:
<Root xmlns="http://www.adventure-works.com">
<Child xmlns="">child content</Child>
</Root>
问题是我不想要 xmlns=""
在我的子元素中(编辑:我不想在我的子元素中出现任何命名空间),我希望它看起来就像第一种方式产生它。我正在构建一个相当大的 XML 文件,因此无法在同一声明中添加所有元素,并且需要能够使用多个 Add
和 SetAttributeValue
方法code>XElements 第一个元素上仍然有 xmlns
。我有什么想法可以做到这一点吗?
谢谢!
I'm having some issues with namespaces in LINQ. So going off of MSDN's Tutorial we can do the following to add namespaces to an XML document with LINQ:
// Create an XML tree in a namespace.
XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "Root",
new XElement(aw + "Child", "child content")
);
Console.WriteLine(root);
Which will give you the XML:
<Root xmlns="http://www.adventure-works.com">
<Child>child content</Child>
</Root>
But what I want to do is this:
XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "Roots");
XElement child = new XElement("Child", "child content");
root.Add(child);
Console.WriteLine(root);
But this gives me the following xml:
<Root xmlns="http://www.adventure-works.com">
<Child xmlns="">child content</Child>
</Root>
The problem is I don't want the xmlns=""
in my child elements (EDIT: I don't want any namespaces in my child elements), I want it to look just like the first way produces it. I am building a fairly large XML file so I can't add all the elements in the same declaration and need to be able to use the Add
and SetAttributeValue
methods of multiple XElements
still having an xmlns
on the first element. Any ideas how I can accomplish this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此行为是 XML 信息集的基础。
xmlns
声明的作用范围为所有子内容;由于您没有为子元素指定命名空间,因此标准行为是将其放置在未命名的命名空间中。为了将元素嵌套在声明默认命名空间的未命名命名空间中,需要使用xmlns=''
声明将默认命名空间重置为未命名。总而言之,根据您的编辑:
xmlns=''
通过 XML 命名空间规范,以便覆盖文档中声明的命名空间元素。xmlns=''
内容),则需要确保 <在代码中,所有这些名称都添加了相同的命名空间。据我所知,没有办法让
XElement
自动继承其父级的命名空间。不过,在构建完 DOM 后,您始终可以对它进行后处理,以在各处设置一个名称空间,类似于以下内容:不过,我认为除了一次性代码之外,不建议这样做,因为它可能会带来不良后果对可维护性的影响。我必须处理许多 XML 文档混合命名空间,而像上面的代码片段那样“破坏”命名空间会破坏混合命名空间文档的语义。
This behavior is fundamental to the XML information set. The effect of the
xmlns
declaration is scoped to all child content; since you did not specify a namespace for the child element, the standard behavior is to place it in the unnamed namespace. In order to nest an element in an unnamed namespace inside of one that declares a default namespace, thexmlns=''
declaration is required to reset the default namespace to unnamed.To summarize, based on your edit:
xmlns=''
is required by the XML Namespaces specification in order to override the namespace declared in the document element.xmlns=''
stuff) in your child elements, you need to make sure the same namespace is added to the names of all of them in code.There is no way I know of to make an
XElement
automatically inherit the namespace of its parent. After you are done building the DOM, though, you can always postprocess it to set one namespace everywhere with something similar to this:I don't believe doing this is advisable for anything other than throwaway code, though, since it will likely have untoward effects on maintainability. A number of XML documents I've had to deal with mix namespaces, and "smashing" namespaces like the above code snippet does would clobber the semantics of a mixed-namespace document.
我能想到的唯一方法是用代码构建整个文档,然后编写一种通用方法将一个名称空间应用于所有元素。这可能很难做到正确,但肯定可能。
基本上
new XElement("Child")
被记录为创建一个没有命名空间的元素 - 您将无法更改它。另一种选择是将所有元素名称提取到变量中(可能是本地变量或静态只读变量)并应用命名空间那里:
然后
The only way I can think of would be to build the whole document up in code and then write a general purpose method to apply one namespace to all elements. That's likely to be awkward to get right, but certainly possible.
Basically
new XElement("Child")
is documented to create an element without a namespace - you're not going to be able to change that.Another option is to extract out all your element names into variables (either local or static readonly, probably) and apply the namespace there:
then