C# 中的 XElement 值

发布于 2024-09-09 08:17:26 字数 467 浏览 3 评论 0原文

如何在不获取子元素的情况下获取 XElement 的值?

一个例子:

<?xml version="1.0" ?>
<someNode>
    someValue
    <child>1</child>
    <child>2</child>
</someNode>

如果我将 XElement.Value 用于 我得到 "somevalue12" 字符串但我只想获得没有 "12" 子字符串的“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

左岸枫 2024-09-16 08:17:26

您可以比使用 Descendants 稍微简单一些 - Nodes 方法仅返回直接子节点:

XElement element = XElement.Parse(
    @"<someNode>somevalue<child>1</child><child>2</child></someNode>");
var firstTextValue = element.Nodes().OfType<XText>().First().Value;

请注意,即使在子元素来自 < 的情况下,这也将起作用。 em>在文本节点之前,如下所示:

XElement element = XElement.Parse(
    @"<someNode><child>1</child><child>2</child>some value</someNode>");
var firstTextValue = element.Nodes().OfType<XText>().First().Value;

You can do it slightly more simply than using Descendants - the Nodes method only returns the direct child nodes:

XElement element = XElement.Parse(
    @"<someNode>somevalue<child>1</child><child>2</child></someNode>");
var firstTextValue = element.Nodes().OfType<XText>().First().Value;

Note that this will work even in the case where the child elements came before the text node, like this:

XElement element = XElement.Parse(
    @"<someNode><child>1</child><child>2</child>some value</someNode>");
var firstTextValue = element.Nodes().OfType<XText>().First().Value;
情泪▽动烟 2024-09-16 08:17:26

没有直接的方法。您必须迭代并选择。例如:

var doc = XDocument.Parse(
    @"<someNode>somevalue<child>1</child><child>2</child></someNode>");
var textNodes = from node in doc.DescendantNodes()
                where node is XText
                select (XText)node;
foreach (var textNode in textNodes)
{
    Console.WriteLine(textNode.Value);
}

There is no direct way. You'll have to iterate and select. For instance:

var doc = XDocument.Parse(
    @"<someNode>somevalue<child>1</child><child>2</child></someNode>");
var textNodes = from node in doc.DescendantNodes()
                where node is XText
                select (XText)node;
foreach (var textNode in textNodes)
{
    Console.WriteLine(textNode.Value);
}
ら栖息 2024-09-16 08:17:26

我认为您想要的是第一个后代节点,因此类似于:

var value = XElement.Descendents.First().Value;

其中 XElement 是表示您的 元素的元素。

您可以专门询问第一个文本元素(即“somevalue”),因此您也可以这样做:

var value = XElement.Descendents.OfType<XText>().First().Value;

I think what you want would be the first descendant node, so something like:

var value = XElement.Descendents.First().Value;

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:

var value = XElement.Descendents.OfType<XText>().First().Value;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文