我已经有了将子项附加到元素的代码。但我还没有找到一种方法来添加另一个兄弟姐妹。基本上我有一个节点列表,它们都是用字母和数字命名的。例如:A01,A02,B05,B06,D19,D20等。现在,当我想将另一个节点添加到“D组”时,到目前为止我有代码执行此操作:
Protected Sub addNodeToSectionInOrder(ByVal rootNode As XmlDocument, ByVal rowNode As XmlElement)
Dim xmlNameTbl As XmlNameTable = rootNode.NameTable
Dim xmlNS As XmlNamespaceManager = New XmlNamespaceManager(xmlNameTbl)
xmlNS.AddNamespace("asp", "http://webadmin.rp.com/asp")
Dim sectionLetter As String = rowNode.GetAttribute("ID").Substring(0, 1)
Dim addingNav As XPathNavigator = rootNode.CreateNavigator
Dim addingItr As XPathNodeIterator = addingNav.Select("//asp:TableRow[starts-with(@ID, '" & sectionLetter & "')]", xmlNS)
While addingItr.MoveNext
'just moving iterator to the last node
'hope to find a better way of doing this
End While
End Sub
现在我只想将rowNode添加到此列表中,作为兄弟。我试过xpathnavigator,xpathnodeiterator,xmlnodelist。到目前为止没有。我很确定我接近错了,但无法弄清楚在哪里。
编辑:示例XML
<?xml version="1.0" encoding="utf-8"?>
<Root>
<BoxGroup ID="A01">
<Items>Apple</Items>
</BoxGroup>
<BoxGroup ID="A02">
<Items>Apple</Items>
</BoxGroup>
<BoxGroup ID="A03">
<Items>Apple</Items>
</BoxGroup>
<BoxGroup ID="B01">
<Items>Apple</Items>
</BoxGroup>
<BoxGroup ID="B02">
<Items>Apple</Items>
</BoxGroup>
<BoxGroup ID="B03">
<Items>Apple</Items>
</BoxGroup>
<BoxGroup ID="C01">
<Items>Apple</Items>
</BoxGroup>
<BoxGroup ID="C02">
<Items>Apple</Items>
</BoxGroup>
<BoxGroup ID="C03">
<Items>Apple</Items>
</BoxGroup>
</Root>
添加为全新节点:
<BoxGroup ID="B04">
<Items>Apple</Items>
</BoxGroup>
结果XML应为:
<?xml version="1.0" encoding="utf-8"?>
<Root>
<BoxGroup ID="A01">
<Items>Apple</Items>
</BoxGroup>
<BoxGroup ID="A02">
<Items>Apple</Items>
</BoxGroup>
<BoxGroup ID="A03">
<Items>Apple</Items>
</BoxGroup>
<BoxGroup ID="B01">
<Items>Apple</Items>
</BoxGroup>
<BoxGroup ID="B02">
<Items>Apple</Items>
</BoxGroup>
<BoxGroup ID="B03">
<Items>Apple</Items>
</BoxGroup>
**<BoxGroup ID="B04">
<Items>Apple</Items>
</BoxGroup>**
<BoxGroup ID="C01">
<Items>Apple</Items>
</BoxGroup>
<BoxGroup ID="C02">
<Items>Apple</Items>
</BoxGroup>
<BoxGroup ID="C03">
<Items>Apple</Items>
</BoxGroup>
</Root>