Convert xml string to DataTable or DataSet in C#

In today’s post, we will explore how to convert XML nodes to a DataTable using C#. XML is a widely used format for data exchange between applications. At times, we may need to parse XML data into our business objects, such as a DataTable. In this tutorial, we will leverage the power of C# to accomplish this task efficiently.


To follow along with this tutorial, you will need a basic understanding of C# and familiarity with XML structures and data manipulation.

Step 1:

Setting up the Environment To begin, create a new ASP.NET Web Forms page (XmlNodeListToDataTable.aspx) and its code-behind file (XmlNodeListToDataTable.aspx.cs). These files will serve as our working environment.

Step 2:

Defining the XML Structure In this example, we will work with an XML file named “Category.xml” that contains a list of categories. Here is a sample XML structure for reference:

<?xml version="1.0" encoding="utf-8"?>
        <MainCategory ID="1">XML</MainCategory>
        <Description>List of XML articles.</Description>
        <MainCategory ID="2">XML1</MainCategory>
        <Description>List of XML articles1.</Description>

DataSet represents an in-memory cache of data.{alertSuccess}

<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="XmlNodeListToDataTable.aspx.cs" Inherits="XmlNodeListToDataTable" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<html xmlns="" >
<head runat="server">
   <title>Untitled Page</title>
   <form id="form1" runat="server">
   <asp:GridView ID="GridView1" runat="server"></asp:GridView>

Step 3:

Loading and Parsing the XML In the Page_Load event of the code-behind file (XmlNodeListToDataTable.aspx.cs), we will load the XML file and retrieve the desired XML nodes. Here’s the code snippet to accomplish this:

public partial class XmlNodeListToDataTable: System.Web.UI.Page {
  protected void Page_Load(object sender, EventArgs e) {

    XmlDocument doc = new XmlDocument();

    //Get and display all the book titles.
    XmlElement root = doc.DocumentElement;
    XmlNodeList elemList = root.GetElementsByTagName("Category");

    GridView1.DataSource = ConvertXmlNodeListToDataTable(elemList);


Step 4:

Converting XmlNodeList to DataTable To convert the XmlNodeList to a DataTable, we will implement the ConvertXmlNodeListToDataTable method. This method iterates over the XML nodes, creates the appropriate columns, and fills the DataTable with the node values. Here’s the implementation:

  public static DataTable ConvertXmlNodeListToDataTable(XmlNodeList xnl) {

    DataTable dt = new DataTable();

    int TempColumn = 0;

    foreach(XmlNode node in xnl.Item(0).ChildNodes) {


      DataColumn dc = new DataColumn(node.Name, System.Type.GetType("System.String"));

      if (dt.Columns.Contains(node.Name)) {

        dt.Columns.Add(dc.ColumnName = dc.ColumnName + TempColumn.ToString());

      } else {




    int ColumnsCount = dt.Columns.Count;
    for (int i = 0; i < xnl.Count; i++) {

      DataRow dr = dt.NewRow();

      for (int j = 0; j < ColumnsCount; j++) {

        dr[j] = xnl.Item(i).ChildNodes[j].InnerText;




    return dt;


Next Post Previous Post
No Comment
Add Comment
comment url