|
-
Feb 18th, 2012, 08:32 PM
#1
Thread Starter
PowerPoster
[RESOLVED] XML parsing - help needed
I know there are a lot of XML methods available and lots of code examples floating around, but for whatever reason I can't wrap my head around exactly what to do. Here's what I have.
I am getting a string of XML data back from a web service. Let's say the string is called strData. The content of strData is structured like this:
<dataset>
<record><field1>blah</field1><field2>whatever</field2></record>
<record><field1>something</field1><field2>somethingelse</field2></record>
</dataset>
So now of course I need to parse this XML to process the data. I am trying to write a little proof-of-concept test app to get this XML loaded into a dataset, defined simply as:
Dim ds As New DataSet
The question is what code / XML objects / methods do I need to populate this dataset, so that I can run the following code to see the field names and corresponding content, like so:
Code:
For Each aRow As DataRow In ds.Tables(0).Rows
For Each aCol As DataColumn In ds.Tables(0).Columns
TextBox2.Text &= aCol.ColumnName & ": " & aRow(aCol) & vbNewLine
Next
TextBox2.Text &= vbNewLine
Next
Any help would be greatly appreciated!
Thanks,
Bruce
Last edited by BruceG; Feb 19th, 2012 at 11:22 AM.
Reason: resolved
"It's cold gin time again ..."
Check out my website here.
-
Feb 18th, 2012, 11:38 PM
#2
Re: XML parsing - help needed
Here is a VS2008 or VS2010 example of parsing XML in a string into a DataTable in a DataSet.
Please note the F1 and F2 can be whatever you want to name them.
Code:
Dim FromWebSource As String = _
"<dataset><record><field1>blah</field1><field2>whatever</field2></record>" & _
"<record><field1>something</field1><field2>somethingelse</field2></record></dataset>"
ds.Tables.Add( _
( _
From D In XDocument.Parse(FromWebSource)...<record> _
Select F1 = D.<field1>.Value, F2 = D.<field1>.Value).ToDataTable _
)
ds.Tables(0).TableName = "MyData"
Place the following into a code module
Code:
<System.Runtime.CompilerServices.Extension()> _
Public Function ToDataTable(Of T)(ByVal value As IEnumerable(Of T)) As DataTable
Dim returnTable As New DataTable
Dim firstRecord = value.First
For Each pi In firstRecord.GetType.GetProperties
returnTable.Columns.Add(pi.Name, pi.GetValue(firstRecord, Nothing).GetType)
Next
For Each result In value
Dim nr = returnTable.NewRow
For Each pi In result.GetType.GetProperties
nr(pi.Name) = pi.GetValue(result, Nothing)
Next
returnTable.Rows.Add(nr)
Next
Return returnTable
End Function
-
Feb 19th, 2012, 08:27 AM
#3
Junior Member
Re: XML parsing - help needed
Try this one:
Code:
Dim responseXDoc As New XDocument
responseXDoc = XDocument.Parse(strData) ' strData is your Xml in a String
Dim ds As New DataSet
ds.ReadXML(responseXDoc)
-
Feb 19th, 2012, 11:20 AM
#4
Thread Starter
PowerPoster
Re: XML parsing - help needed
Thank you both for your help.
Kevin, with all due respect and appreciation, your solution was a little "over the top" for me, plus it seemed from the SELECT portion that knowledge of all the column names in advance was required (which I was hoping to avoid).
phytax
A "short and sweet" solution like yours is what I was hoping for - however I ran into a problem because the ds.ReadXml method does not accept the XDocument object - but the good news is, after searching around, I found how to fix it -here is the final code (to get from the XML string to a dataset):
Code:
Dim responseXDoc As New XDocument
responseXDoc = XDocument.Parse(strData) ' strData is your Xml in a String
Dim objReader As XmlReader = responseXDoc.CreateReader
Dim ds As New DataSet
ds.ReadXML(objReader)
"It's cold gin time again ..."
Check out my website here.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|