My XML code is like this:
<?xml version="1.0" encoding="utf-8"?>
<Tabel>
<Member>
<Naam>Cruciatum</Naam>
<Kills>1000</Kills>
<Deaths>10</Deaths>
<KD>100</KD>
</Member>
<Member>
<Naam>Ghostbullet93</Naam>
<Kills>10</Kills>
<Deaths>1</Deaths>
<KD>10</KD>
</Member>
<Member>
<Naam>test</Naam>
<Kills>123</Kills>
<Deaths>11</Deaths>
<KD>11</KD>
</Member>
</Tabel>
After processing, the XML should end up looking like this:
<?xml version="1.0" encoding="utf-8"?>
<Tabel>
<Member>
<Naam>Cruciatum</Naam>
<Kills>1000</Kills>
<Deaths>10</Deaths>
<KD>100</KD>
</Member>
<Member>
<Naam>Ghostbullet93</Naam>
<Kills>10</Kills>
<Deaths>1</Deaths>
<KD>10</KD>
</Member>
</Tabel>
After a bit of searching I came up with this code.
Apparently it worked for others, yet it just won’t work for me at all.
Private Sub btnDel_Click(sender As System.Object, e As System.EventArgs) Handles btnDel.Click
playername = lstmembers.SelectedItem.ToString
If MsgBox("Ben je zeker dat je " & playername & " wil verwijderen?", MsgBoxStyle.YesNo, "Confirmatie") = MsgBoxResult.Yes Then
Dim xmldoc As New XmlDocument()
xmldoc.load("C:\members.xml")
Dim node As XmlNode = xmldoc.SelectSingleNode("Root/Naam[. = '" & playername & "']")
If node IsNot Nothing Then
node.ParentNode.RemoveChild(node)
xmldoc.Save("C:\members.xml")
End If
'reload list
loadfile()
End If
End Sub
I don’t get any exceptions, so the code must be right for something, just not for what I need apparently…
I hope you can see where I made my mistake.
Look at your XPath expression:
That’s expecting an element called
Root. Your element is actually calledMember, assuming you’ve really given us a representative file. You may needTabel/Member/Naam– I can’t remember whether using XPath on a document implicitly starts at the document root element or not.I haven’t checked the rest of your code, but it’s at least worth trying that first. It looks like your code will actually just remove the
Naamelement, by the way – I think you may want:(Or extract
node.ParentNodeto a separate local variable, of course.)Personally I would use LINQ to XML if at all possible – it would make life simpler – but that’s a different matter.