Querying XML file and create result XML file using C#.NET OR ASP.NET

Previous                                                        Home                                           

Querying XML file and create result XML file

In this example, i am showing how to query XML file using dataview and create the another XML file as output.

Steps:
  1. Create one input XML file. 
  2. load XML file into dataset. 
  3. load dataview with dataset table and with conditions(query) as shown below.. 
  4. create one xml file from the dataview as output. 

Input XML file: 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 with conditions
            DataView objDV = new DataView(ds.Tables[0],"sal>300000","ename",DataViewRowState.CurrentRows);

            //loading dataview into customer xml file. 
            objDV.ToTable().WriteXml("customer.xml");

            Console.WriteLine("xml file created.");
            Console.ReadLine();
        }
    }
}



Output XML file: customer.xml
<?xml version="1.0" standalone="yes"?>
<DocumentElement>
  <Table>
    <eno>4</eno>
    <ename>john</ename>
    <sal>310000</sal>
    <location>Bangalore</location>
  </Table>
  <Table>
    <eno>2</eno>
    <ename>swati</ename>
    <sal>330000</sal>
    <location>Bangalore</location>
  </Table>
</DocumentElement>

See more XML ASP.NET Tutorials:

Previous                                                        Home        
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