【问题标题】:How i can read xml into DataTable type object?我如何将 xml 读入 DataTable 类型的对象?
【发布时间】:2013-11-14 09:36:05
【问题描述】:

如果我将 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];

【问题讨论】:

    标签: c# .net xml datatable dataset


    【解决方案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 - datatable

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-13
        • 1970-01-01
        相关资源
        最近更新 更多