XML 到数据集 - 查找数据行包含数据表的问题
抱歉,如果描述不好,但我不知道还能怎么说...... 但这里是一个 XML 结构的示例,
<?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>
我已将其转换为数据集,以便以数据表的形式访问信息。 但我的问题是试图获取错误代码。我试图通过检查表的关系来弄清楚这一点。因为我必须弄清楚哪些错误代码与哪个人相关,以便正确显示它们。我无法控制 XML 结构,为了减少多余的带宽,一切都是可选的,因此目前无法选择更改它。但这是我所做的一个示例:
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
这为我提供了 errorCodes 表内数据的实际值。但我不知道如何将其链接回与其关联的行/人。关系生成的索引是根据其出现情况构建的(例如,susan 的错误代码为 0,peter 的错误代码为 1),与它们在 XML 中的位置无关。
我要么做错了,要么错误地查看了这些数据。 我知道我可以使用 XML 阅读器来获取所有这些,但数据已经在数据集中,我想学习一些新东西。所以请仅建议将其作为最后的手段。
提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您的 XML 格式不正确 - 快速复制并粘贴到 .NET 项目中的 xml 文档中将向您显示: 首先。
如果您可以解决这个问题:如果您使用 ReadMode.Auto 执行 ReadXml 操作,那么您应该能够执行以下操作。请注意,.NET 添加了额外的列来维护关系,我现在已经对它们进行了硬编码:
更好的方法可能是循环遍历信息表,然后使用数据视图来过滤错误代码和错误代码表找到您感兴趣的代码:
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:
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:
我最终只是通读了 XML,检查了节点名称,并为错误代码构建了一个单独的表,当我找到信息的结束元素时,我单独吐出了这些表。
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.