Searching data from XML file using c#.net or ASP.NET

Previous                                                        Home                                                 Next

Searching data from XML file

In this example, i am searching data item from the XML file. For the below program, i am giving XML file as input and then i am searching data from that XML file.

Steps:
  1. give EmployeeDetails.xml as input file. 
  2. loading xml file into dataset. 
  3. load dataset table into dataview. 
  4. search data in the dataview with required name. 
Input:EmployeeDetails.xml
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <Table>
    <eno>1</eno>
    <ename>ranga</ename>
    <sal>260000</sal>
    <location>Hyderabad</location>
  </Table>
  <Table>
    <eno>2</eno>
    <ename>swati</ename>
    <sal>330000</sal>
    <location>Bangalore</location>
  </Table>
  <Table>
    <eno>3</eno>
    <ename>anil</ename>
    <sal>220000</sal>
    <location>chennai</location>
  </Table>
  <Table>
    <eno>4</eno>
    <ename>john</ename>
    <sal>310000</sal>
    <location>Bangalore</location>
  </Table>
  <Table>
    <eno>5</eno>
    <ename>Kavitha</ename>
    <sal>110000</sal>
    <location>Bangalore</location>
  </Table>
  <Table>
    <eno>6</eno>
    <ename>padma</ename>
    <sal>25000</sal>
    <location>Bangalore</location>
  </Table>
</NewDataSet>


Program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace searching_data_in_xml_file
{
    class Program
    {
        static void Main(string[] args)
        {
            string filePath = "EmployeeDetails.xml";
            DataSet ds = new DataSet();
            //reading xml file into dataset. 
            ds.ReadXml(filePath);

            //loading dataset into dataview.
            DataView objDV = new DataView(ds.Tables[0]);

            
            objDV.Sort = "ename";
            int position=objDV.Find("ranga");

            if (position == -1)
            {
                Console.WriteLine("data not found");
            }
            else
            {
                Console.WriteLine(objDV[position]["ename"].ToString());
            }
            Console.ReadLine();
        }
    }
}


Output:
ranga



See more XML tutorial:



Previous                                                        Home                                                 Next
Share this post :

Post a Comment

Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.

 
Support : Ranga Rajesh Kumar
Copyright © 2012. ASP.NET Examples - All Rights Reserved
Site Designed by Ranga Rajesh Kumar