我写了以下代码:
Imports System.Xml
Imports System.Security.Cryptography
Imports System.IO
Public Class FormMain
Private Sub btnGo_Click(sender As System.Object, e As System.EventArgs) Handles btnGo.Click
Dim ds1 As New DataSet
ds1 = NewGetRecords()
DataGridView1.DataSource = ds1.Tables(0)
Label1.Text = ds1.Tables(0).TableName
If ds1.Tables.Count > 1 Then
DataGridView2.DataSource = ds1.Tables(1)
Label2.Text = ds1.Tables(1).TableName
End If
End Sub
Public Function NewGetRecords() As DataSet
Dim ds As New DataSet
Dim xd As New XmlDocument
xd.LoadXml(TextBox1.Text)
ds.ReadXml(New XmlNodeReader(xd))
Return ds
End Function
End Class
这是我正在使用的 XML 数据(这有效):
<?xml version="1.0"?><ABMDataSets><ABDataSet><OwnerLogin>demo</OwnerLogin><OwnerUniqueID>abcd</OwnerUniqueID><OwnerPassword>abcd</OwnerPassword><OwnerName>Demo Owner</OwnerName><Address1>324 East Wisconsin Avenue</Address1><Address2>Suite 423</Address2><Address3></Address3><City>Milwaukee</City><State>WI</State><Country>US</Country><PostalCode>53202</PostalCode><Phone>414-431-8775</Phone><Email>abcd@efg.com</Email><PayPalAccount>abcd@efg.com</PayPalAccount><PercentMarkup>5</PercentMarkup><JoinDate>2012-01-25 00:57:00</JoinDate><ReferredBy></ReferredBy><Active>1</Active></ABDataSet></ABMDataSets>
当我有多条记录时不起作用(例如,有两条类似于上面的记录):
<?xml version="1.0"?><ABMDataSets><ABDataSet><OwnerLogin>demo</OwnerLogin><OwnerUniqueID>abcd</OwnerUniqueID><OwnerPassword>abcd</OwnerPassword><OwnerName>Demo Owner</OwnerName><Address1>324 East Wisconsin Avenue</Address1><Address2>Suite 423</Address2><Address3></Address3><City>Milwaukee</City><State>WI</State><Country>US</Country><PostalCode>53202</PostalCode><Phone>414-431-8775</Phone><Email>abcd@efg.com</Email><PayPalAccount>abcd@efg.com</PayPalAccount><PercentMarkup>5</PercentMarkup><JoinDate>2012-01-25 00:57:00</JoinDate><ReferredBy></ReferredBy><Active>1</Active><OwnerLogin>demo</OwnerLogin><OwnerUniqueID>abcd</OwnerUniqueID><OwnerPassword>abcd</OwnerPassword><OwnerName>Demo Owner</OwnerName><Address1>324 East Wisconsin Avenue</Address1><Address2>Suite 423</Address2><Address3></Address3><City>Milwaukee</City><State>WI</State><Country>US</Country><PostalCode>53202</PostalCode><Phone>414-431-8775</Phone><Email>abcd@efg.com</Email><PayPalAccount>abcd@efg.com</PayPalAccount><PercentMarkup>5</PercentMarkup><JoinDate>2012-01-25 00:57:00</JoinDate><ReferredBy></ReferredBy><Active>1</Active></ABDataSet></ABMDataSets>
第一组数据为我提供了一个名为 ABDataset 的漂亮表,所有字段都按预期列在列中。它只给了我一张桌子和一张记录。第二组数据应该给我相同的两条记录,但给我一些 19 或 20 个表,字段名称、列名称和值之间没有明显的关系。