我在每个语句中都有3个循环遍历xml文档中的法规( aobjXMLCaseDoc ),然后检查这些法规是否符合以下法规260C.405,518.131,518B.01,609.748 ,629.75
我想将每个这三个组合成一个用于每个语句。 我该怎么做?
XML
<?xml version="1.0" encoding="UTF-8"?>
<Charge>
<ChargeHistory ChargeHistoryID="387">
<Statute>
<StatuteNumber>609.352.2a(2)</StatuteNumber>
</Statute>
</ChargeHistory>
<ChargeHistory ChargeHistoryID="398">
<Statute>
<StatuteNumber>609.352.2a(2)</StatuteNumber>
</Statute>
</ChargeHistory>
<ChargeHistory ChargeHistoryID="517">
<Statute>
<StatuteNumber>609.352.2a(2)</StatuteNumber>
</Statute>
</ChargeHistory>
<Deleted>false</Deleted>
</Charge>
VB代码
'Check if statute has 8 characters
For Each objXMLStatuteNode In aobjXMLCaseDoc.DocumentElement.SelectNodes("Case/Charge/ChargeHistory/Statute/StatuteNumber")
If objXMLStatuteNode.InnerText.Length >= 8 Then
strStatuteEightDigits = objXMLStatuteNode.InnerText.Substring(0, 8)
Select Case strStatuteEightDigits
Case "260C.405"
blnNotSpecifiedStatute = False
Case Else
blnNotSpecifiedStatute = True
End Select
End If
Next
'Check if statute has 7 characters
For Each objXMLStatuteNode In aobjXMLCaseDoc.DocumentElement.SelectNodes("Case/Charge/ChargeHistory/Statute/StatuteNumber")
If objXMLStatuteNode.InnerText.Length >= 7 Then
strStatuteSevenDigits = objXMLStatuteNode.InnerText.Substring(0, 7)
Select Case strStatuteSevenDigits
Case "518.131", "518B.01", "609.748"
blnNotSpecifiedStatute = False
Case Else
blnNotSpecifiedStatute = True
End Select
End If
Next
'Check if statute has 6 characters
For Each objXMLStatuteNode In aobjXMLCaseDoc.DocumentElement.SelectNodes("Case/Charge/ChargeHistory/Statute/StatuteNumber")
If objXMLStatuteNode.InnerText.Length >= 6 Then
strStatuteSixDigits = objXMLStatuteNode.InnerText.Substring(0, 6)
Select Case strStatuteSixDigits
Case "629.75"
blnNotSpecifiedStatute = False
Case Else
blnNotSpecifiedStatute = True
End Select
End If
Next
答案 0 :(得分:1)
使用If-ElseIf:
For Each objXMLStatuteNode In aobjXMLCaseDoc.DocumentElement....
If objXMLStatuteNode.InnerText.Length >= 8 Then
''' Checks for 8 or more chars
....
ElseIf objXMLStatuteNode.InnerText.Length = 7 Then
''' Checks for 7 Chars
....
ElseIf objXMLStatuteNode.InnerText.Length = 6 Then
''' Checks for 6 chars
....
Else
''' For Less than 6 chars
End If
Next
另外我认为你的逻辑可能需要一些调整,因为8是&gt; = 8,但它也是&gt; = 7并且也是&gt; =。
答案 1 :(得分:1)
您的所有代码似乎都在查看是否有任何法规编号开始,其中包含五个字符串之一(“260C.405”,“518.131”,“518B.01” ,“609.748”,“629.75”)。您可以使用StartsWith
来执行此操作。
您可以使用Any
检查是否有任何法规编号符合某项测试,该测试也可以使用Any
查看法规编号是否以任何字符串列表开头。整个过程可以在一个声明中完成。
您提供的示例XML以<Charge>
开头,但您的代码似乎假设<Charge>
元素存在于<Case>
元素中。为了保持一致,我已从代码中删除了<Case>
。如果需要,您需要将其放回(紧接<Charge>
之前)。
请注意,您的代码(以及我的代码)会检查以“629.75”开头的任何法规编号,因此“629.751”将被视为匹配。您可能希望检查以“629.75”开头的法规编号。而不是“629.75”
Private statutes As New List(Of String) From {"260C.405", "518.131", "518B.01", "609.748", "629.75"}
Private objXML As XDocument = <?xml version="1.0" encoding="UTF-8"?>
<Charge>
<ChargeHistory ChargeHistoryID="387">
<Statute>
<StatuteNumber>609.352.2a(2)</StatuteNumber>
</Statute>
</ChargeHistory>
<ChargeHistory ChargeHistoryID="398">
<Statute>
<StatuteNumber>609.352.2a(2)</StatuteNumber>
</Statute>
</ChargeHistory>
<ChargeHistory ChargeHistoryID="517">
<Statute>
<StatuteNumber>609.352.2a(2)</StatuteNumber>
</Statute>
</ChargeHistory>
<Deleted>false</Deleted>
</Charge>
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim blnNotSpecifiedStatute As Boolean
blnNotSpecifiedStatute = Not objXML.<Charge>.<ChargeHistory>.<Statute>.<StatuteNumber>.Any(Function(n) statutes.Any(Function(s) n.Value.StartsWith(s)))
'Do something with blnNotSpecifiedStatute
End Sub