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

11,227

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
}
Share:
11,227
Răzvan Flavius Panda
Author by

Răzvan Flavius Panda

Updated on August 22, 2022

Comments

  • Răzvan Flavius Panda
    Răzvan Flavius Panda almost 2 years

    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!