帮助从xml中删除节点 [英] help removing a node from xml

查看:88
本文介绍了帮助从xml中删除节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的xml文件,我整天都向其中添加信息.有时我需要删除一个节点,所以我必须进入文件以删除它已经很旧的文件.我也有下面的代码,我试图用来删除信息,它不会删除正在寻找的节点.请帮忙.

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 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>





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

推荐答案

您的XPath查询不正确.名称不是属性而是元素.应该是这样的:

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''] 


这篇关于帮助从xml中删除节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆