我已经重做了这个问题,因为有几个建议很难理解我的意思,因此稍微减少了这个问题。
<?xml version="1.0"?>
<root>
<succesfulResponses>
<position>0</position>
<response>
<dln>BBUTU204250VS9VT</dln>
<licence>
<entitlements>
<code>A</code>
<validFrom/>
<validTo/>
<priorTo>false</priorTo>
<type>F</type>
</entitlements>
<entitlements>
<code>B</code>
<validFrom/>
<validTo/>
<priorTo>false</priorTo>
<type>F</type>
</entitlements>
</licence>
</response>
</succesfulResponses>
<succesfulResponses>
<position>1</position>
<response>
<dln>BTXRS755313Y99AT</dln>
<licence>
<entitlements>
<code>A</code>
<validFrom>2003-02-28</validFrom>
<validTo>2043-05-30</validTo>
<priorTo>false</priorTo>
<type>P</type>
</entitlements>
<entitlements>
<code>AM</code>
<validFrom>2014-05-14</validFrom>
<validTo>2043-05-30</validTo>
<priorTo>false</priorTo>
<type>P</type>
</entitlements>
</licence>
<httpStatusCode>200</httpStatusCode>
</response>
</succesfulResponses>
</root>
Run Code Online (Sandbox Code Playgroud)
这是我返回的XML,我向服务提交了多个ID,它返回了该XML。
如果我发送2个ID,则为每个ID返回2个“ successfulResponses”元素,您可以在“响应”下面的子节点“ dln”中看到这些ID,您会发现它们是不同的。
“位置”就是我首先在“请求”中提交的ID。“响应”还具有一个称为“许可证”的元素和多个“授权”元素。
我希望将这些“权利”插入以这种方式格式化的数据表中
Results.EntitlementsTbl.Columns.Add(New DataColumn("Code", GetType(String)))
Results.EntitlementsTbl.Columns.Add(New DataColumn("Valid From", GetType(String)))
Results.EntitlementsTbl.Columns.Add(New DataColumn("Valid To", GetType(String)))
Results.EntitlementsTbl.Columns.Add(New DataColumn("Prior To", GetType(String)))
Results.EntitlementsTbl.Columns.Add(New DataColumn("Type", GetType(String)))
Results.EntitlementsTbl.Columns.Add(New DataColumn("Driver", GetType(String)))
Run Code Online (Sandbox Code Playgroud)
每列都与每个“权利”元素下方的节点相关。每当我有一个“权利”元素时,我都希望在数据表中添加一个新行。
还有一个附加的“驱动程序”列,需要在“位置”中填充数字。
所以我的输出到我的表我想有以下内容:
我希望这比我以前的讲得更有意义。我已经删除了我已经声明完成的任何自己的代码,因为这显然不是必需的。
我衷心推荐第二个建议,将JSON直接反序列化为CLR对象,您可能还会发现使用XDocumentAPI 更容易,但是如果XmlDocument出于某些原因确实必须使用模型,那么此代码应该会有所帮助
Dim DriverNo As String
Using nodes As XmlNodeList = xmlDoc.DocumentElement.SelectNodes("/root/succesfulResponses")
For Each node In nodes
Dim DLN As String = ""
DriverNo = node.SelectSingleNode("position").InnerText
DLN = node.SelectSingleNode("response/dln").InnerText.ToString()
Using entitlements As XmlNodeList = node.SelectNodes("response/licence/entitlements")
For Each entitlement In entitlements
Dim code, validFrom, validTo, priorTo, type As String
code = entitlement.SelectSingleNode("code").InnerText
validFrom = entitlement.SelectSingleNode("validFrom").InnerText
validTo = entitlement.SelectSingleNode("validTo").InnerText
priorTo = entitlement.SelectSingleNode("priorTo").InnerText
type = entitlement.SelectSingleNode("type").InnerText
' do what you need to with the variables here
Next
End Using
Next
End Using
Run Code Online (Sandbox Code Playgroud)
这样,您将遍历每个successfulResponses节点,获取DLN和DriverNo,然后遍历每个entitlements节点并从那里获取数据。这将导致每个权利一行。
该do what you need to with the variables here会是你的实际代码插入到您的数据表,或者你使用任何。