How to select a node using XPathNavigator.SelectSingleNode(string xpath)?

Răzvan Flavius Panda picture Răzvan Flavius Panda · Aug 5, 2011 · Viewed 11.1k times · Source

I have this xml file "target.xml":

<World>
  <Nkvavn>
    <Rcltwkb>
      <Pjwrgsik />
      <Nemscmll />
      <Fnauarnbvw />
      <Egqpcerhjgq />
      <Olyhryyxi />
      <Vvlhtiee />
      <Wlsfhmv />
    </Rcltwkb>
    <Xudbhnakjb>
      <Cwxjtkteuji />
      <Fbtcvf />
      <Uviaceinhl />
    </Xudbhnakjb>
    <Kgujcymilwr>
      <Nlbvgtwoejo />
      <Tvufkvmryybh />
      <Xtomstcenmp />
      <Mhnngf />
      <Fjidqdbafxun />
    </Kgujcymilwr>
    <Taiyiclo>
      <Fiecxoxeste />
      <Loqxjq />
      <Vfsxfilxofe />
      <Hroctladlht />
    </Taiyiclo>
  </Nkvavn>
  <Tfrosh>
    <Tuqomkytlp>
      <Oyvivlvminhn />
      <Qeypvfgul />
      <Mbapjl />
    </Tuqomkytlp>
    <Rvxumtj>
      <Gkvigncdvgy />
      <Okcddyi />
      <Vvmacul />
    </Rvxumtj>
    <Pdjpgexuyc>
      <Yvsdmbckurju />
      <Bvkxvg />
      <Clmrvjwk />
      <Hdafjhydj />
      <Asauxtnoe />
      <Mwcviwmi />
    </Pdjpgexuyc>
  </Tfrosh>
</World>

In the method BindCities(string country) i am trying to get to the country element () but the nav variable doesn't change it's value to the country element after running the code, it just stays at the last location. I tried a lot of methods but nothing worked.

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;

namespace MultipleBoundListBox
{
    public partial class Form1 : Form
    {
        private static XmlDocument xmlDoc;
        private static XPathNavigator nav;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            xmlDoc = new XmlDocument();
            xmlDoc.Load(@"target.xml");

            nav = xmlDoc.DocumentElement.CreateNavigator();

            nav.MoveToFirstChild();
            var countries = new List<string>();
            countries.Add(nav.LocalName);

            while (nav.MoveToNext())
            {
                countries.Add(nav.LocalName);
            }

            listBox1.DataSource = countries;

            BindCities(countries[0]);
        }

        protected void BindCities(string country)
        {
            nav.MoveToRoot();
            var xpath = "//" + country;
            nav.SelectSingleNode(xpath);
            nav.MoveToFirstChild();

            var cities = new List<string>();
            cities.Add(nav.LocalName);

            while (nav.MoveToNext())
            {
                cities.Add(nav.LocalName);
            }

            listBox2.DataSource = cities;
        }
    }
}

What code do i need to reach the country element with the nav XPathNavigator?

Thanks for your replies!

Answer

Martin Honnen picture Martin Honnen · Aug 5, 2011

The proper use of the SelectSingleNode method is as follows:

XPathNavigator node = nav.SelectSingleNode(xpath);
if (node != null) {
  // now access properties of node here e.g. node.LocalName
}
else {
  // if needed handle case that xpath did not select anything
}