在VB.Net中如何将XmlNodeReader与多个相同类型的项一起使用?

时间:2013-04-03 15:30:36

标签: xml vb.net linq-to-xml

我写了以下代码:

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

1 个答案:

答案 0 :(得分: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()代码 - 非常好!