C# 中的 XElement 值
如何在不获取子元素的情况下获取 XElement
的值?
一个例子:
<?xml version="1.0" ?>
<someNode>
someValue
<child>1</child>
<child>2</child>
</someNode>
如果我将 XElement.Value 用于
我得到 "somevalue
字符串但我只想获得没有 "
子字符串的“somevalue”。
How to get a value of XElement
without getting child elements?
An example:
<?xml version="1.0" ?>
<someNode>
someValue
<child>1</child>
<child>2</child>
</someNode>
If i use XElement.Value for <someNode>
I get "somevalue<child>1</child><child>2<child>"
string but I want to get only "somevalue" without "<child>1</child><child>2<child>"
substring.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以比使用 Descendants 稍微简单一些 - Nodes 方法仅返回直接子节点:
请注意,即使在子元素来自 < 的情况下,这也将起作用。 em>在文本节点之前,如下所示:
You can do it slightly more simply than using
Descendants
- theNodes
method only returns the direct child nodes:Note that this will work even in the case where the child elements came before the text node, like this:
没有直接的方法。您必须迭代并选择。例如:
There is no direct way. You'll have to iterate and select. For instance:
我认为您想要的是第一个后代节点,因此类似于:
其中
XElement
是表示您的
元素的元素。您可以专门询问第一个文本元素(即“somevalue”),因此您也可以这样做:
I think what you want would be the first descendant node, so something like:
Where
XElement
is the element representing your<someNode>
element.You can specifically ask for the first text element (which is "somevalue"), so you could also do: