Click here to Skip to main content
13,589,789 members
Click here to Skip to main content
Add your own
alternative version


3 bookmarked
Posted 22 Feb 2011
Licenced CPOL

Merge DataTable from XML in ASP.NET

, 22 Feb 2011
Rate this:
Please Sign up or sign in to vote.
Merge DataTable from XML in ASP.NET
We are using two XML (studentmaster.xml and resultmaster.xml) to join in DataTable.

HTML Block

Past below code in form tag of ASP.NET default.aspx page.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"/>

XML Part

Create studentmaster.xml in root folder.
  <student id="1001" name="PUNEET JAIN" />
  <student id="1002" name="RAKESH MEENA" />
  <student id="1003" name="PRAKASH MEHTA" />
  <student id="1004" name="RAJENDRA SINGH" />
  <student id="1005" name="MOHIT SHARMA" />

Create resultmaster.xml in root folder.
  <result hindi="45" english="75" maths="55" total="175" percent="58.33" division="second" id="1001" />
  <result hindi="65" english="65" maths="90" total="220" percent="73.33" division="first" id="1002" />
  <result hindi="36" english="65" maths="75" total="176" percent="58.66" division="second" id="1003" />
  <result hindi="80" english="85" maths="65" total="230" percent="76.66" division="first" id="1004" />
  <result hindi="75" english="55" maths="55" total="185" percent="61.66" division="first" id="1005" />

Code Part

Past below code in default.aspx.cs page. GetDataTable() function to read records from XML, in our example we have two XMLs studentmaster.xml and resultmaster.xml both XML in root folder. GetMergerDataTable() function has accepted two datatables, then second datatable merges in first datatable. Finally call Clear() function of DataTable Class to clear from memory for second datatable. First, add all columns of second table in first table, then add all rows value of second table in first datatable.
protected void Page_Load(object sender, EventArgs e)
    if (!IsPostBack)
        DataTable dtStudentMaster = GetDataTable("studentmaster.xml");
        DataTable dtStudentResult = GetDataTable("resultmaster.xml");
        if (GetMergeDataTable(dtStudentMaster, dtStudentResult))
        { GridView1.DataSource = dtStudentMaster; }
//function to read record from xml and return datatable.
protected DataTable GetDataTable(string sXMLFileName)
    DataTable dtT = null;
        string strFilePath = Server.MapPath("") + @"\" + sXMLFileName;
        DataSet dsTemp = new DataSet();
        if (dsTemp.Tables.Count > 0) { dtT = dsTemp.Tables[0]; }
    catch (Exception ex) { }
    return dtT;

//function for merging two datatable.
protected bool GetMergeDataTable(DataTable dtFirtsMergeWithSecond, DataTable dtSecond)
    bool retVal = false;
        DataColumn dcT = null; int iRC = 0;

        //Get new column info from second datatable to add in first.
        DataColumnCollection dccT = dtSecond.Columns;
        foreach (DataColumn dcTT in dccT)

            dcT = new DataColumn(dcTT.ColumnName+"_m", dcTT.DataType);
        // fill row value in first datatable from second datatable.
        DataRowCollection drcT = dtSecond.Rows;
        iRC = 0;
        foreach (DataRow drTT in drcT)
            dtFirtsMergeWithSecond.Rows[iRC]["hindi"+"_m"] = drTT["hindi"];
            dtFirtsMergeWithSecond.Rows[iRC]["english" + "_m"] = drTT["english"];
            dtFirtsMergeWithSecond.Rows[iRC]["maths" + "_m"] = drTT["maths"];
            dtFirtsMergeWithSecond.Rows[iRC]["total" + "_m"] = drTT["total"];
            dtFirtsMergeWithSecond.Rows[iRC]["percent" + "_m"] = drTT["percent"];
            dtFirtsMergeWithSecond.Rows[iRC]["division" + "_m"] = drTT["division"];
            dtFirtsMergeWithSecond.Rows[iRC]["id" + "_m"] = drTT["id"];
            iRC = iRC + 1;
        if (iRC == dtSecond.Rows.Count)
            retVal = true;
            //All rows in all tables are removed.
    catch (Exception ex) { }
    return retVal;


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

R S Dodiya
Technical Lead Wipro Ltd.
India India

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.180618.1 | Last Updated 23 Feb 2011
Article Copyright 2011 by R S Dodiya
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid