如果一个节点属于一个命名空间,那么它的子节点默认属于同一个命名空间。因此,无需为每个子级提供 xmlns
属性,这很好。
然而。
如果我像这样创建两个节点:
Dim parent = <parent xmlns="http://my.namespace.org"/>
Dim child = <child xmlns="http://my.namespace.org">value</child>
parent.Add(child)
Console.WriteLine(parent.ToString)
结果是这样的:
<parent xmlns="http://my.namespace.org">
<child xmlns="http://my.namespace.org">value</child>
</parent>
但是,如果以不太方便的方式创建它们:
Dim parent = <parent xmlns="http://my.namespace.org"/>
Dim child As New XElement(XName.Get("child", "http://my.namespace.org")) With {.Value = "value"}
parent.Add(child)
Console.WriteLine(parent.ToString)
结果更理想:
<parent xmlns="http://my.namespace.org">
<child>value</child>
</parent>
显然,我更喜欢使用第一种方式,因为它更加直观和简单编码。不使用方法 2 还有另一个原因 - 有时我需要使用 XElement.Parse
创建节点,解析包含 xmlns
属性的字符串,这会产生完全相同的结果就像方法 1 一样。
所以问题是——如何获得方法 2 的漂亮输出,像方法 1 一样创建节点?我看到的唯一选择是创建一个方法来克隆给定的 XElement,根据方法 2 模式有效地重新创建它,但这看起来很难看。我正在寻找一个更明显的解决方案,但由于某种原因而被我忽略了。
If a node belongs to a namespace, it's children by default belong to the same namespace. So there's no need to provide an xmlns
attribute on each child, which is good.
However.
If I create two nodes like this:
Dim parent = <parent xmlns="http://my.namespace.org"/>
Dim child = <child xmlns="http://my.namespace.org">value</child>
parent.Add(child)
Console.WriteLine(parent.ToString)
The result is this:
<parent xmlns="http://my.namespace.org">
<child xmlns="http://my.namespace.org">value</child>
</parent>
But, if create them in a less convenient way:
Dim parent = <parent xmlns="http://my.namespace.org"/>
Dim child As New XElement(XName.Get("child", "http://my.namespace.org")) With {.Value = "value"}
parent.Add(child)
Console.WriteLine(parent.ToString)
The result is more desirable:
<parent xmlns="http://my.namespace.org">
<child>value</child>
</parent>
Obviously, I'd prefer to use the first way because it is so much more intuitive and easy to code. There's also another reason to not use method 2 -- sometimes I need to create nodes with XElement.Parse
, parsing a string that contains an xmlns
attribute, which produces exactly same results as method 1.
So the question is -- how do I get the pretty output of method 2, creating nodes as in method 1? The only option I see is to create a method that would clone given XElement, effectively recreating it according to method 2 pattern, but that seems ugly. I'm looking for a more obvious solution I overlooked for some reason.
发布评论
评论(1)
呃...命名空间 - 它们会害死我。
开始吧:
为了能够使用
XElement.Parse
并使子节点与其父节点的命名空间保持同步,最好使用全局命名空间。在 VB.NET 中确实很容易做到。在模块/类的顶部,只需使用导入,所有父级和子级都将使用此命名空间。例如:请注意,首先创建
元素。这同样适用于非默认命名空间,例如Imports
然后使用和
。我曾经读过,但尚未再次找到,将 XML Literals 与 I-don-know-what-you-call-it-but-it's-that-
parent.Add(something)
-这不是一个好主意。Ugh...namespaces - they will be the death of me.
Here you go:
To be able to use
XElement.Parse
and keep child nodes in sync with their parent nodes' namespaces, it's best to use global namespaces. Really easy to do in VB.NET. At the top of your module/class, just use an Imports and all parents and children will use this namespace. For example:Note that the
<child/>
element is created first. The same would apply to a non-default namespace, likeImports <xmlns:p="http://parent.namespace.org">
and then creating with<p:child/>
and<p:parent/>
.I once read, but have yet to find again, that mixing XML Literals with I-don't-know-what-you-call-it-but-it's-that-
parent.Add(something)
-thing is a bad idea.