简体   繁体   中英

VB.Net write and read DataTable to/from XML-file

I'm trying to write a DataTable to an XML-file and read afterwards the data from the XML-file to another DataTable with the following VB.Net code, I'm getting the error "DataTable does not support schema inference from Xml.":

dt1.WriteXml(fileName:=xf, writeHierarchy:=True)  
Dim dt2 = New Data.DataTable(dt1.TableName)  
dt2 = ds.Tables(0)  
dt2.ReadXml(fileName:=xf)  

I could solve my problem with read the file into a DataSet, but I would like to understand the difference:

Dim ds = New Data.DataSet()  
ds.ReadXml(fileName:=xf)  
Dim dt2 = ds.Tables(0)  

Could anybody tell me?

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

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

Example :

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

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")

And then populate Your GridView or what else You need.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM