0

我写了以下代码:

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 个表,字段名称、列名称和值之间没有明显的关系。

4

1 回答 1

1

我认为您需要一个额外的级别来封装每条记录,我<rec></rec>在这里使用过:

<?xml version="1.0" encoding="utf-8"?><ABMDataSets><ABDataSet><rec><OwnerLogin>demo</OwnerLogin><OwnerUniqueID>abcd</OwnerUniqueID><OwnerPassword>abcd</OwnerPassword><OwnerName>Demo Owner</OwnerName><Address1>324 East Wisconsin Avenue</Address1><Address2>Suite 423</Address2><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/><Active>1</Active></rec><rec><OwnerLogin>demo</OwnerLogin><OwnerUniqueID>abcd</OwnerUniqueID><OwnerPassword>abcd</OwnerPassword><OwnerName>Demo Owner</OwnerName><Address1>324 East Wisconsin Avenue</Address1><Address2>Suite 423</Address2><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/><Active>1</Active></rec></ABDataSet></ABMDataSets>

或将每条记录包装在<ABDataSet></ABDataSet>

我稍微修改了您的代码,以一次显示所有表格:

            Dim miCnt As Integer

            Private Sub btnGo_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
                Dim ds1 As New DataSet
                ds1 = NewGetRecords()
                Dim iTbl As Integer = miCnt Mod ds1.Tables.Count
                DataGridView1.DataSource = ds1.Tables(iTbl)
                Label1.Text = ds1.Tables(iTbl).TableName
                miCnt += 1
            End Sub

感谢您的 NewGetRecords() 代码 - 非常好!

于 2013-04-03T20:15:39.210 回答