且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

VB.Net向/从XML文件读写DataTable

更新时间:2023-11-24 16:52:22

我知道很久以前就问过这个问题,但是几天前我也遇到了同样的问题。

I know this question asked long time ago, but I had same problem few days ago.

您必须设置 TableName $ DataTable 的c $ c>在导出之前(写 xml )。

You have to set TableName for DataTable before exporting (writing xml).

示例:

    dt1.TableName = "MyDataTable"
    dt1.WriteXmlSchema(Application.StartupPath + "\test_sh.xml", True)
    dt1.WriteXml(Application.StartupPath + "\test_dt.xml", True)

然后读取(从 xml 导入)回到新的 DataTable 中:

And, for read (import from xml) back in new DataTable :

    dt2 = New DataTable
    dt2.ReadXmlSchema(Application.StartupPath + "\test_sh.xml")
    dt2.ReadXml(Application.StartupPath + "\test_dt.xml")

然后填充您的 GridView 或您还需要什么。

And then populate Your GridView or what else You need.

TableName dt2 将自动从架构文件( test_sh.xml )中提取。在这种情况下, MyDataTable 就像在 dt1.TableName 中设置的一样。

TableName for dt2 will be automatically pulled from schema file (test_sh.xml). In this case MyDataTable, like was set in dt1.TableName.

保存 schema 也是重要,否则您将无法阅读 xml 返回表格。

It's important to save schema, too, or You can't read xml back in table.