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

Total Post:105

Points:739
Posted on    January-30-2014 11:06 PM

 C# C# 
Ratings:


 1 Reply(s)
 916  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();

temp.ReadXml(UncompressedStream);

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

Points:2881
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();

    newTable.ReadXml(fileName);

    // 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;

    table.Columns.Add(column);

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

    table.Columns.Add(column);

    // Add ten rows.

    DataRow row;

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

    {

        row = table.NewRow();

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

        table.Rows.Add(row);

    }

    table.AcceptChanges();

    return table;

}


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

Follow MindStick