刪除所有第二個元素我用下面的XML實例工作:VB.net XML按標記名稱
<order>
<name></name>
<etc></etc>
<etc></etc>
<components>
<component>
<sub></sub>
</component>
<component>
<sub></sub>
</component>
<component>
<sub></sub>
</component>
<component>
<sub></sub>
</component>
</components>
</order>
我試圖選擇並刪除每秒<component>
,它在vb.net的sub
秒。
下面是我到目前爲止有:
'make xmldocument object to hold xml string'
Dim XMLDoc As New XMLDocument()
XMLDoc.LoadXML(strXMLContent)
'get all elements named component'
Dim componentList As XmlNodeList = XMLDoc.GetElementsByTagName("Component")
Dim c As Integer
Dim i As Integer = 1
'for every component element add i to itself to only get seconds'
'and if on a second element, remove it and all sub elements inside it'
For c = 0 To componentList.Count - 1
i += i
If i > 2 Then
componentList(c).ParentNode.RemoveChild(componentList(c))
End If
Next c
'output to a string to see results'
x &= XMLDoc.DocumentElement.OuterXml
我遇到的問題是,我得到「Object reference not set to an instance of an object
」當我運行這一點,在實際RemoveChild()
被稱爲線。
你能描述你遇到的問題嗎?你的問題不清楚哪裏出了問題。 – FluffyKitten
當我運行這個時,我得到「對象引用未設置爲對象的實例」。我不確定在刪除孩子的路線上我應該使用什麼邏輯。 – Evohc
您應該直接在問題中包含任何信息,因爲有些人可能會錯過評論。這次我爲你編輯了這個問題。這不是我的專業領域,我只是審查你的問題(所有的第一次問題都經過審查),所以現在看來是一個很好的問題,我會讓專家回答:) – FluffyKitten