...">

XPath does not work as expected

I have an XML document in this format:

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

And I am trying to parse an XML file as follows:

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);
        }
    });
}

The problem I am facing is both values ​​(name and active) that will look like Google WeatherYes , when all I want is Google Weather , Can someone tell me what happened to my XPath (which is here) :

InitializeDropDown("SupportedWeatherServices.xml", "descendant::Service[name]");
+3
source share
1 answer

XPath should be //Service/name

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

or descendant::Service/nameif you like this syntax more.

+4
source

All Articles