gpt4 book ai didi

c# - XPath 未按预期工作

转载 作者:数据小太阳 更新时间:2023-10-29 02:27:45 24 4
gpt4 key购买 nike

我有一个这种格式的 XML 文档:

<?xml version="1.0" encoding="utf-8" ?>
<SupportedServices>
<Service>
<name>Google Weather</name>
<active>Yes</active>
</Service>
...
</SupportedServices>

我正在尝试像这样解析 XML 文件:

public void InitializeDropDown(string XmlFile, string xpath)
{
XmlDocument doc = new XmlDocument();
doc.Load(XmlFile);

var rootNode = doc.DocumentElement;

var serviceList = rootNode.SelectNodes(xpath);

Parallel.ForEach(serviceList.Cast<XmlNode>(), service =>
{
if (Properties.Settings.Default.ServiceActive &&
Properties.Settings.Default.ServiceName == service.InnerText)
{
WeatherServicesCBO.Items.Add(service.InnerText);
}
});
}

我遇到的问题是两个值(名称和事件)都被选中,所以它看起来像 Google WeatherYes,而我想要的只是 Google Weather .谁能告诉我我的 XPath(在此处)出了什么问题:

InitializeDropDown("SupportedWeatherServices.xml", "descendant::Service[name]");

最佳答案

XPath 应该是 //Service/name

var serviceList = rootNode.SelectNodes("//Service/name");

descendant::Service/name,如果你更喜欢这种语法。

关于c# - XPath 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6302176/

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