我寫了下面的代碼:在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個表,字段名稱,列名和值之間沒有明顯的關係。
請詳細解釋當它「不起作用」時會發生什麼。你有錯誤嗎?如果是這樣的話,你的代碼中哪一條語句正確? 'xd.LoadXml'是否給出錯誤?或者當你做'ds.ReadXml'時DataSet? –
對不起,忘了添加錯誤的解釋,現在已經做到了...... – Chiwda