HOW I CAN READ XML INTO DATATABLE TYPE OBJECT?

Total Post:110

Points:774
C# 
C#
 1365  View(s)
Ratings:
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];
  1. Post:412

    Points:2888
    Re: How i can read xml into DataTable type object?

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

      Modified On Apr-04-2018 02:27:23 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!