我有一个带有多个faultIsolationProcedures的XML文档,我需要增加ID号。该代码的工作原理是,它对每个faultIsolationProcedure进行3次计数。我尝试使用不同的XPath表达式,但不断收到奇怪的3时间循环。
代码:
Dim fiCount = 1
Dim renumNodes As XmlNode = xmldoc.SelectSingleNode("/dmodule/content/faultIsolation/faultIsolationProcedure")
For Each fi In renumNodes
Dim origFiId = renumNodes.Attributes("id").Value
Dim newFIID = "f" + fiCount.ToString
renumNodes.Attributes(0).Value = modExistID
dictionaryRenumberID.Add(origFiId, newFIID)
fiCount = fiCount + 1
Next
具有单一故障的XML示例
<content>
<faultIsolation>
<faultIsolationProcedure applicRefId="test" id="-1">
<isolationStep id="i1-001">
<isolationStepQuestion></isolationStepQuestion>
<isolationStepAnswer>
<yesNoAnswer>
<yesAnswer nextActionRefId="s1-003" />
<noAnswer nextActionRefId="e1-002" />
</yesNoAnswer>
</isolationStepAnswer>
</isolationStep>
<isolationProcedureEnd id="e1-002">
</isolationProcedureEnd>
<isolationStep id="i1-001a">
</isolationStep>
<isolationProcedureEnd id="e1-007">
</isolationProcedureEnd>
</faultIsolationProcedure>
</faultIsolation>
</content>
答案 0 :(得分:1)
使用我发现更容易的XElement。首先是数据
Dim xe As XElement
' xe=XElement.Load("path here")
'for testing use a literal
xe = <content>
<faultIsolation>
<faultIsolationProcedure applicRefId="test" id="-1">
<isolationStep id="i1-001">
<isolationStepQuestion></isolationStepQuestion>
<isolationStepAnswer>
<yesNoAnswer>
<yesAnswer nextActionRefId="s1-003"/>
<noAnswer nextActionRefId="e1-002"/>
</yesNoAnswer>
</isolationStepAnswer>
</isolationStep>
<isolationProcedureEnd id="e1-002">
</isolationProcedureEnd>
<isolationStep id="i1-001a">
</isolationStep>
<isolationProcedureEnd id="e1-007">
</isolationProcedureEnd>
</faultIsolationProcedure>
<faultIsolationProcedure applicRefId="test" id="">
<isolationProcedureEnd id="e1-007">
</isolationProcedureEnd>
</faultIsolationProcedure>
</faultIsolation>
</content>
请注意,我添加了一个faultIsolationProcedure。
然后使用该数据,id可以像这样递增
Dim ie As IEnumerable(Of XElement)
'get all faultIsolationProcedure
ie = From el In xe...<faultIsolationProcedure> Select el
'increment id
For Each f As XElement In ie
Dim idv As Integer = 0
If Integer.TryParse(f.@id, idv) Then
f.@id = (idv + 1).ToString
Else
Dim att As XAttribute = f.Attribute("id")
If att IsNot Nothing Then
f.@id = "1"
End If
End If
Next