.xml数据表函数的奇怪行为 [英] Strange behavior of the .xml datatable functions
问题描述
有一阵子,我使用两种格式处理.xml和.csv文件,但当时没有遇到这种现象.现在,我很难理解发生了什么事.
我为验证而创建的代码.只是创建一个虚拟数据表,存储在.xml中,然后将该.xml重新下载到数据表中.
私有子Button1_Click(作为对象发送者,作为EventArgs发送者)处理Button1.Click
将dt dt转换为DataTable =新的DataTable("DataTable")
对于x = 0到5
dt.Columns.Add(列" + x.ToString)
下一步
对于x = 0到5
对于y = 0到10
dt.Rows.Add()
dt.Rows(y).Item(x)= x.ToString + y.ToString
下一步
下一步
dt.WriteXml("d:\ test.xml")
dt.Dispose()
结束子项
Private Sub Button2_Click(作为对象发送,作为EventArgs发送)处理Button2.Click
dt.ReadXmlSchema("d:\ test.xml")
dt.ReadXml("d:\ test.xml")
MessageBox.Show("Columns:" + dt.Columns.Count.ToString +; Rows:" + dt.Rows.Count.ToString)
dt.Dispose()
结束子
For a while I worked with .xml and .csv files using both formats, but such behavior is not encountered at the time. Now it's difficult for me to understand what was going on.
That code I created for verification. It's just creating a dummy datatable, stored in .xml and download this .xml into datatable again.
Private Sub Button1_Click (sender As Object, e As EventArgs) Handles Button1.Click
Dim x, y As Integer: Dim s As String = String.Empty
Dim dt As DataTable = New DataTable ("DataTable")
For x = 0 To 5
dt.Columns.Add ("Column" + x.ToString)
Next
For x = 0 To 5
For y = 0 To 10
dt.Rows.Add ()
dt.Rows (y) .Item (x) = x.ToString + y.ToString
Next
Next
dt.WriteXml ("d: \ test.xml")
dt.Dispose ()
End Sub
Private Sub Button2_Click (sender As Object, e As EventArgs) Handles Button2.Click
Dim dt As DataTable = New DataTable ("DataTable")
dt.ReadXmlSchema ("d: \ test.xml")
dt.ReadXml ("d: \ test.xml")
MessageBox.Show ("Columns:" + dt.Columns.Count.ToString + "; Rows:" + dt.Rows.Count.ToString)
dt.Dispose ()
End Sub
很明显,将有6列和12行(带有标题).但是MessageBoxs显示字符串"Columns:6;".行:66".怎么了? 66从何而来?
It is clear that there will be 6 columns and 12 rows (with a title). But MessageBoxs shows the string "Columns: 6; Rows: 66". What's the matter? From where 66 coming?
推荐答案
你好,
您应该查看生成的xml文件,该文件将指示您没有正确创建行.尝试以下
You should look at the resulting xml file which will indicate you were not creating the rows correctly. Try the following
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim x, y As Integer : Dim s As String = String.Empty
Dim dt As DataTable = New DataTable("DataTable")
For x = 0 To 5
dt.Columns.Add("Column" + x.ToString)
Next
For x = 0 To 5
dt.Rows.Add()
For y = 0 To 5
dt.Rows(x).Item(y) = x.ToString + y.ToString
Next
Next
End Sub
End Class
这篇关于.xml数据表函数的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!