Home > DeveloperSection > Forums > Search in a XML file in C#
Jeet Verma
Jeet Verma

Total Post:110

Points:776
Posted on    June-15-2013 5:15 AM

 C# C# 
Ratings:


 1 Reply(s)
 847  View(s)
Rate this:
Hi Experts,

How to Search in a XML file?

Thanks in advance


Sumit Kesarwani

Total Post:378

Points:2694
Posted on    June-15-2013 7:22 AM

Hi Jeet,

You can use this code:

using System;

using System.Data;

using System.Windows.Forms;

using System.Xml;

 

namespace WindowsApplication1

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            XmlReader xmlFile;

            xmlFile = XmlReader.Create("Product.xml", new XmlReaderSettings());

            DataSet ds = new DataSet();

            DataView dv;

            ds.ReadXml(xmlFile);

 

            dv = new DataView(ds.Tables[0]);

            dv.Sort = "Product_Name";

            int index = dv.Find("Product2");

 

            if (index == -1)

            {

                MessageBox.Show("Item Not Found");

            }

            else

            {

                MessageBox.Show(dv[index]["Product_Name"].ToString() + " " + dv[index]["Product_Price"].ToString());

 

            }

        }

    }

}


Don't want to miss updates? Please click the below button!

Follow MindStick