krish
krish

Reputation: 9

how to fetch node elements from XML file by using xpath query,

i want to fetch elements through search pattern like if i type "an" then i want all elements which have "an" example =man, animal, fan, pant

this is my code here i use foreach loop to display all search elements but i don't want to use foreach loop just i want to fetch all the list directly form xpath query please help me out its very impotent for me

private void Search2_Click_1(object sender, EventArgs e)
        {


            XmlNodeList nodes = myxml.DocumentElement.SelectNodes("/students/student/s_name" );

            string ha = search.Text;

            if (listbox11.Text == "Name")
            foreach(XmlNode node in nodes)
            {

                if(System.Text.RegularExpressions.Regex.IsMatch(node.InnerText,ha))
                { 
                    listBox1.Text += node.InnerText + "\r\n"; 
                }

            }

        }

Upvotes: 1

Views: 145

Answers (2)

krish
krish

Reputation: 9

**The code which i write is simple, xpath query will fetch only related elements nodes but if you want to print then use foreach loop **

private void Search2_Click_1(object sender, EventArgs e) {

       string ha = search.Text;

        if (listbox11.Text == "Name")
        {
            listBox1.Text = "";

            XmlNodeList nodes = myxml.DocumentElement.SelectNodes("//s_name[descendant-or-self::*[contains(.,'" + ha + "')]]");
            foreach (XmlNode node in nodes)
            {


                    listBox1.Text += node.InnerText + "\r\n";


            }
        }

    }

Upvotes: 0

Gokulakannan
Gokulakannan

Reputation: 128

Use this

private void Search2_Click_1(object sender, EventArgs e)
        {
  string ha = search.Text;

XmlNodeList nodes = myxml.DocumentElement.SelectNodes("/students/student/[contains(s_name,ha)]");

}

Upvotes: 1

Related Questions