Home > DeveloperSection > Forums > How i can read xml into DataTable type object?
Jayden Bell
Jayden Bell

Total Post:105

Posted on    January-30-2014 11:06 PM

 C# C# 

 1 Reply(s)
 894  View(s)
Rate this:

I don't have problem if i read xml to DataSet:

DataSet temp = new DataSet();

temp.ReadXml(UncompressedStream, System.Data.XmlReadMode.Auto);

But if i use DataTable:

DataTable temp = new DataTable();


then datatable not load data (count of columns and rows equal 0 )

How i can read xml to DataTable

My temporary solution:

DataSet temp = new DataSet();

DataTable structure = new DataTable();

temp.ReadXml(UncompressedStream, System.Data.XmlReadMode.Auto);

structure = temp.Tables[0];

Pravesh Singh

Total Post:411

Posted on    January-30-2014 11:30 PM

Hi Jayden,

Try this:

private static void DemonstrateReadWriteXMLDocumentWithString()


    DataTable table = CreateTestTable("XmlDemo");

    PrintValues(table, "Original table");

    string fileName = "C:\\TestData.xml";

    table.WriteXml(fileName, XmlWriteMode.WriteSchema);

    DataTable newTable = new DataTable();


    // Print out values in the table.

    PrintValues(newTable, "New table");


private static DataTable CreateTestTable(string tableName)


    // Create a test DataTable with two columns and a few rows.

    DataTable table = new DataTable(tableName);

    DataColumn column = new DataColumn("id", typeof(System.Int32));

    column.AutoIncrement = true;


    column = new DataColumn("item", typeof(System.String));


    // Add ten rows.

    DataRow row;

    for (int i = 0; i <= 9; i++)


        row = table.NewRow();

        row["item"] = "item " + i;




    return table;


Don't want to miss updates? Please click the below button!

Follow MindStick