gpt4 book ai didi

c# - 如何测试节点是否包含特定字符串或字符作为其文本值?

转载 作者:太空宇宙 更新时间:2023-11-03 22:22:29 25 4
gpt4 key购买 nike

如何使用 C# 代码测试节点是否包含特定字符串或字符。

例子:

<abc>
<foo>data testing</foo>
<foo>test data</foo>
<bar>data value</bar>
</abc>

现在我需要测试特定节点值是否包含字符串“testing”?

输出将是“foo[1]”

最佳答案

您还可以将其放入 XPath 文档中,然后使用查询:

var xPathDocument = new XPathDocument("myfile.xml");
var query = XPathExpression.Compile(@"/abc/foo[contains(text(),""testing"")]");

var navigator = xpathDocument.CreateNavigator();
var iterator = navigator.Select(query);

while(iterator.MoveNext())
{
Console.WriteLine(iterator.Current.Name);
Console.WriteLine(iterator.Current.Value);
}

关于c# - 如何测试节点是否包含特定字符串或字符作为其文本值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2560835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com