1

如果我将 xml 读取为DataSet

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

但如果我使用DataTable

DataTable temp = new DataTable();
temp.ReadXml(UncompressedStream);

然后数据表不加载数据(列数和行数等于 0 )

我如何读取 xml 到DataTable

我的临时解决方案:

DataSet temp = new DataSet();
DataTable structure = new DataTable();
temp.ReadXml(UncompressedStream, System.Data.XmlReadMode.Auto);
structure = temp.Tables[0];
4

2 回答 2

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

这来自:microsoft - xml - 数据表

检查在此代码中,他们为表添加了名称,并创建了行和列。

于 2013-11-14T09:42:34.580 回答
0

LINQ to XML可能是您正在寻找的东西。

于 2013-11-14T09:40:40.923 回答