CSharp.Net-Informations.com
   Home      .Net Framework      VB.NET      C#                                                                      About


  How to search in a XML file

XML files are made up of tags that contains information. The .Net technology is widely supported XML file format. Also the Dataset in ADO.NET uses XML format as its internal storage format.

The following C# source code shows how to search an item in a XML file using Dataset . Here Dataset using an XmlReader for read the content of the file. Locate the XML file using XmlReader and pass the XmlReader as argument of Dataset. By using the Dataset , search the product Product2 in the file Product.XML with the help of DataView.

         C# Source Code Download           Print Source Code
         How to search in a XML file - Download
        
C# Tutorial

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

            }
        }
    }
}

Click here to download the input file product.xml : product.xml

C# XML Related Contents
*     How to XML in C#
*     How to create an XML file in C#
*     How to open and read an XML file in C#
*     How to create XML file from Dataset
*     How to Open and read an XML file to Dataset
*     How to create an XML file from SQL
*     How to filter in a XML file
*     How to insert data from XML to database
*     How to create Excel file from XML
*     How to create XML file from Excel
*     How to XML to DataGridView
*     How to create a TreeView from XML File
*     How to create a Crystal Reports from XML File
*     XML Serialization Tutorial
*     XML Serialization
*     XML DeSerialization


   Home      VB.NET      C#
CSharp Related Topics
*     An overview of Microsoft CSharp
*     C# Language Tutorial
*     C# Statements Tutorial
*     C# Collection Tutorial
*     C# String Tutorial
*     C# File Operations Tutorial
*     C# Excel Tutorial
*     C# Crystal Reports Tutorial
*     CSharp Communication Tutorial
*     C# Ado.Net Tutorial and Source Code
*     C# ADO.NET data Providers Tutorial
*     C# Dataset Tutorial
*     C# DataAdapater Tutorial
*     Csharp DataView Tutorial
*     Csharp Remoting Tutorial
*     C# XML Tutorial
*     C# DataGridView Tutorial
   Home      VB.NET      C#
More Source Code :   
Mail to :  feedback@net-informations.com
  |  Home   |  VB.NET   |  C#   |  SiteMap   |  Terms of Use   |  About   |
net-informations.com (C) 2010
All Rights Reserved. All other trademarks are property of their respective owners.