XML to dataset - problem finding what datarow contains a datatable
Sorry if the description is poor, but I don't know how else to put this... But here is an example of the XML structure
<?xml version=”1.0” encoding=”UTF-8”>
<Response xmlns="http://www.blah.com">
<searchResult>
<info>
<firstName>John</firstName>
<lastName>Doe</lastName>
<totalCharges>100.00</totalCharges>
<nonPaymentCode>99999</nonPaymentCode>
</info>
<info>
<firstName>Susan</firstName>
<lastName>Doe</lastName>
<totalCharges>1000.00</totalCharges>
<errorCodes>
<errorCode>12345</errorCode>
</errorCodes>
</info>
<info>
<firstName>Peter</firstName>
<lastName>Doe</lastName>
<totalCharges>10.00</totalCharges>
<errorCodes>
<errorCode>12345</errorCode>
<errorCode>54321</errorCode>
<errorCode>85246</errorCode>
</errorCodes>
</info>
</searchResult>
</claimInquiryResponse>
I have transformed this into a dataset in order to access the information as a datatable. But my problem is trying to get the errorCodes. I was trying to figure this out by checking the relationship of the tables. Because I have to figure out what error codes are associated with what person, in order to display them properly. I cannot control the XML structure, and everything is optional in order to reduce excess bandwidth so changing it is not an option at this point. But here is an example of what I have done:
For Each rel As DataRelation In ds.Tables(i).ChildRelations
If rel.Nested Then
Dim temp As New DataTable
temp = rel.ChildTable
For Each relationship As DataRelation In temp.ChildRelations
For Each row As DataRow In relationship.ChildTable.Rows
For Each column As DataColumn In relationship.ChildTable.Columns
rowValues &= column.ColumnName & "-" & row(column.ColumnName) & "-" & relationship.RelationName & vbCrLf
Next
Next
Next
End If
Next
This gives me the actual values of the data inside of errorCodes table. but I can't figure out how to link it back to what row/person it was associated with. The indexes the relationship generate are built off of their occurance (for instance the error codes for susan will be 0 and for peter it will be 1), not relating to their location in the XML.
I am either doing this wrong or looking at this data incorrectly. I know I can use a XML reader to get all this, but the data is already in the dataset, and I feel like开发者_运维百科 learning something new. so only suggest it as a last resort please.
thanks in advance!
First point is that your XML isn't well formed - a quick copy and paste into an xml document in the your .NET project will show you that: <Response> </claimInquiryResponse>
for a start.
If you can sort that out then: If you perform a ReadXml operation with ReadMode.Auto then you should be able to do the following. Note that extra columns are added by .NET to maintain the relations and I have hard coded these for now:
Dim strfile As String = "somefile.xml"
Dim ds As New DataSet
ds.ReadXml(strfile, XmlReadMode.Auto)
For Each drInfo As DataRow In ds.Tables("info").Rows
Debug.Print(drInfo.Item("lastname").ToString + " " + drInfo.Item("firstname").ToString)
For Each drCodes As DataRow In ds.Tables("errorCodes").Rows
If drCodes.Item("info_Id").ToString = drInfo.Item("info_Id").ToString Then
For Each drCodeDetail As DataRow In ds.Tables("errorCode").Rows
If drCodeDetail.Item("errorCodes_Id").ToString = drCodes.Item("errorCodes_Id").ToString Then
Debug.Print(" Code ->" + drCodeDetail.Item("errorCode_Text").ToString)
End If
Next
End If
Next
Next
A better way to do this is probably to loop through the info table and then use a dataview to filter the errorCodes and the errorCode table to find the codes you are interested in:
Dim dvCodes As New DataView, dvCodeDetail As New DataView
dvCodes = ds.Tables("errorCodes").DefaultView
dvCodeDetail = ds.Tables("errorCode").DefaultView
For Each drInfo As DataRow In ds.Tables("info").Rows
Debug.Print(drInfo.Item("lastname").ToString + " " + drInfo.Item("firstname").ToString)
dvCodes.RowFilter = "info_Id = " + drInfo.Item("info_Id").ToString
For i As Integer = 0 To dvCodes.Count - 1
dvCodeDetail.RowFilter = "errorCodes_Id = " + dvCodes(i).Item(0).ToString
For j As Integer = 0 To dvCodeDetail.Count - 1
Debug.Print(" Code ->" + dvCodeDetail(j).Item(0).ToString)
Next
Next
Next
I ended up just reading through the XML checking the node name and building a separate table for the error codes, and when I found the end element for info I spat out the tables individually.
精彩评论