【发布时间】:2011-06-10 05:04:34
【问题描述】:
我有一个这种格式的 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]");
【问题讨论】: