我只能阅读和编辑第一个声明,但我需要代码在所有声明中执行相同操作。它必须在每个索赔中计算行数并在每行中写入行号。下面是xml文件;
<ClaimBatch>
<Claims>
<Claim>
<claimGross>4947.7200</claimGross>
<numLines>2</numLines>
<Line>
<lineNo>1</lineNo>
<benefitAmount>2473.8600</benefitAmount>
<levy>247.3860</levy>
</Line>
<Line>
<lineNo>2</lineNo>
<benefitAmount>2473.8600</benefitAmount>
<levy>247.3860</levy>
</Line>
</Claim>
<Claim>
<claimGross>549.6800</claimGross>
<numLines>2</numLines>
<Line>
<benefitAmount>274.8400</benefitAmount>
<levy>48.0976</levy>
</Line>
<Line>
<benefitAmount>274.8400</benefitAmount>
<levy>48.0976</levy>
</Line>
</Claim>
</Claims>
</ClaimBatch>
以下是我目前使用的代码;
Public Sub writeLineNo()
Dim counter As Integer = 1
Dim reader As XmlReader = XmlReader.Create("C:\SQLQUERIS\" & cmbschemetxt & " " & dateStr & ".xml")
xmlDoc = XDocument.Load("C:\SQLQUERIS\" & cmbschemetxt & " " & dateStr & ".xml")
For Each line In xmlDoc.<ClaimBatch>.<Claims>.<Claim>.<Line>
If reader.ReadToFollowing("Line") Then
reader.MoveToContent()
While reader.ReadToNextSibling("Line")
line.AddFirst(<lineNo><%= counter %></lineNo>)
counter += 1
End While
End If
Next
reader.Close()
xmlDoc.Save("C:\SQLQUERIS\" & cmbschemetxt & " " & dateStr & ".xml")
End Sub
答案 0 :(得分:0)
只需使用两个For Each
循环。一个循环遍历每个<Claim>
,一个循环遍历每个<line>
内的每个<Claim>
。
给出以下XML
Dim xml = <ClaimBatch>
<Claims>
<Claim>
<claimGross>4947.7200</claimGross>
<numLines>2</numLines>
<Line>
<benefitAmount>2473.8600</benefitAmount>
<levy>247.3860</levy>
</Line>
<Line>
<benefitAmount>2473.8600</benefitAmount>
<levy>247.3860</levy>
</Line>
</Claim>
<Claim>
<claimGross>549.6800</claimGross>
<numLines>2</numLines>
<Line>
<benefitAmount>274.8400</benefitAmount>
<levy>48.0976</levy>
</Line>
<Line>
<benefitAmount>274.8400</benefitAmount>
<levy>48.0976</levy>
</Line>
</Claim>
</Claims>
</ClaimBatch>
使用嵌套循环,如:
For Each claim in xml.<Claims>.<Claim>
Dim counter = 1
For Each line in claim.<Line>
line.AddFirst(<lineNo><%= counter %></lineNo>)
counter += 1
Next
Next
xml
现在是: