Here is an example of an XML file. You can have any of your existing XML or create a new XML file.
- <Employees>
- <NO1>
- <Name>aaa</Name>
- <ZIP>333</ZIP>
- <Address>aaa</Address>
- <City>aa City</City>
- <State>aa</State>
- </NO1>
- <NO2>
- <Name>bbb</Name>
- <ZIP>239</ZIP>
- <Address>bbb</Address>
- <City>bb City</City>
- <State>bbb</State>
- </NO2>
- <NO3>
- <Name>ccc</Name>
- <ZIP>100</ZIP>
- <Address>ccc</Address>
- <City>Bcc City</City>
- <State>ccc</State>
- </NO3>
- </Employees>
Now, create a new Web application using Visual Studio and add two GridView control to the default WebForm. We're going to load XML into two different GridView controls based on a filter on zip code.
The following code loads xml file into a DataSet then creates two DataTable objects.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data;
- public partial class _Default : System.Web.UI.Page
- {
- DataRow row2;
- protected void Page_Load(object sender, EventArgs e)
- { }
- protected void Button1_Click(object sender, EventArgs e)
- {
- DataSet ds = new DataSet();
- ds.ReadXml(Server.MapPath("~/test.xml"));
- DataTable dt = new DataTable();
- DataTable dt1 = new DataTable();
- for (int i = 0; i < ds.Tables.Count; i++)
- {
- if (Convert.ToInt32(ds.Tables[i].Rows[0]["ZIP"].ToString()) > 200)
- {
- dt.Merge(ds.Tables[i]);
- }
- else
- {
- dt1.Merge(ds.Tables[i]);
- }
- }
- GridView1.DataSource = dt;
- GridView1.DataBind();
- GridView2.DataSource = dt1;
- GridView2.DataBind();
- }
- }
Build and run application.