Click here to Skip to main content
15,443,631 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have the below xml file and i add information to it through out the day. Sometimes i need to remove a node so i have to go into the file to delete it which is getting very old. I have the below code as well that i m trying to use to delete the information and it wont remove the node im looking for. Please help.

XML
<?xml version="1.0" encoding="utf-8" ?>
- <!-- XML Database.
  -->
- <AdminTools>
- <App>
  <Name>Test1</Name>
  <Path>C:\Users\zachary.shupp\Documents\Tools\Zack's MMC.msc</Path>
  <Icon>5</Icon>
  </App>
- <App>
  <Name>Test2</Name>
  <Path>C:\Users\zachary.shupp\Documents\Tools\Zack's MMC.msc</Path>
  <Icon>1</Icon>
  </App>
  </AdminTools>


VB
Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click

    ' Load the XmlDocument.
    Dim xd As New XmlDocument()
    xd.Load(Application.StartupPath & "\Tools.xml")

    Dim nod As XmlNode = xd.SelectSingleNode("/AdminTools/app[@Name='Test1']")
    If nod IsNot Nothing Then

        nod.ParentNode.RemoveChild(nod)
        MessageBox.Show("Deleted")
    Else

        MessageBox.Show("Unable to Delete")
    End If

    ' Save the Xml.
    xd.Save(Application.StartupPath & "\Tools.xml")

End Sub
Posted

1 solution

Your XPath query isn't correct. Name is not an attribute but an element. It should be something like this:

/AdminTools/App/Name[text()='Test1'] 
 
Share this answer
 
Comments
Zachary.shupp 13-Jul-12 12:19pm     CRLF
Thank you so much i did make the change it didnt work like i thought it would. Instead of deleting the whole group it deleted just the name from that group. How can i get it to delete the wholegroup for test1?
[no name] 13-Jul-12 12:46pm    
Try RemoveAll();
Zachary.shupp 13-Jul-12 13:57pm    
I did tray RemoveAll and it does remove all but not the APP /APP. Any more Ideas?
Prasad_Kulkarni 16-Jul-12 5:13am    
My 5!
Manas Bhardwaj 16-Jul-12 5:54am    
thx!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900