2013-04-03 522 views
0

我寫了下面的代碼:在VB.Net如何使用XmlNodeReader與多個相同類型的項目?

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>[email protected]</Email><PayPalAccount>[email protected]</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>[email protected]</Email><PayPalAccount>[email protected]</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>[email protected]</Email><PayPalAccount>[email protected]</PayPalAccount><PercentMarkup>5</PercentMarkup><JoinDate>2012-01-25 00:57:00</JoinDate><ReferredBy></ReferredBy><Active>1</Active></ABDataSet></ABMDataSets> 

的第一組數據給了我一個很好的表稱爲ABDataset和所有的領域都在列如預期。它只給我一張桌子和一張唱片。第二組數據應該給我兩條記錄,但是我給了我19或20個表,字段名稱,列名和值之間沒有明顯的關係。

+0

請詳細解釋當它「不起作用」時會發生什麼。你有錯誤嗎?如果是這樣的話,你的代碼中哪一條語句正確? 'xd.LoadXml'是否給出錯誤?或者當你做'ds.ReadXml'時DataSet? –

+0

對不起,忘了添加錯誤的解釋,現在已經做到了...... – Chiwda

回答

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>[email protected]</Email><PayPalAccount>[email protected]</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>[email protected]</Email><PayPalAccount>[email protected]</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()代碼 - 非常好!

+0

這很有道理。我希望有一個更簡單的方法,因爲我將不得不去改變數據庫中的每條記錄。但那是我的錯 - 糟糕/懶惰的數據設計。 – Chiwda

+0

順便說一句,你的解決方案沒有多個表的工作 - 我在一張表中獲得多行。代碼沒有變化,只是改變了數據格式。 – Chiwda

相關問題