如何使用XmlDocument查找元素然后删除整个节点?

时间:2014-07-15 17:04:27

标签: xml vb.net

无法遍历XML文档以找到某个元素,以便我可以 删除整个节点。

我的xml看起来像:

<?xml version="1.0" encoding="utf-8"?>
<FileZillaServer>
  <Groups />
  <Users>
    <User Name="jbrown">
      <Option Name="Pass">2ac9cb7dc02b3c0083eb70898e549b63</Option>
      <Option Name="ForceSsl">0</Option>
      <Option Name="8plus3">0</Option>
      <IpFilter>
        <Disallowed />
        <Allowed />
      </IpFilter>
      <Permissions>
        <Permission Dir="C:\inetpub\wwwroot">
          <Option Name="FileRead">1</Option>
          <Option Name="DirList">1</Option>
          <Option Name="DirSubdirs">1</Option>
          <Option Name="AutoCreate">0</Option>
        </Permission>
      </Permissions>
      <SpeedLimits DlType="0" DlLimit="10" ServerDlLimitBypass="0" UlType="0" UlLimit="10" ServerUlLimitBypass="0">
        <Download />
        <Upload />
      </SpeedLimits>
    </User>
    <User Name="3-Private">
      <Option Name="Pass">test5</Option>
      <Option Name="ForceSsl">0</Option>
      <Option Name="8plus3">0</Option>
      <IPFilter>
        <Disallowed />
        <Allowed />
      </IPFilter>
      <Permissions>
        <Permission Dir="C:\Backup Spaces\3\files">
          <Option Name="FileRead">1</Option>
          <Option Name="DirList">1</Option>
          <Option Name="AutoCreate">0</Option>
        </Permission>
      </Permissions>
      <SpeedLimits ServerUlLimitBypass="0" UlLimit="10" UlType="0" ServerDlLimitBypass="0" DlLimit="10" DlType="0">
        <Download />
        <Upload />
      </SpeedLimits>
    </User>
    <User Name="3-Public">
      <Option Name="Pass">test6</Option>
      <Option Name="ForceSsl">0</Option>
      <Option Name="8plus3">0</Option>
      <IPFilter>
        <Disallowed />
        <Allowed />
      </IPFilter>
      <Permissions>
         <Permission Dir="C:\Backup Spaces\3\files">
         <Option Name="FileRead">1</Option>
         <Option Name="DirList">1</Option>
         <Option Name="AutoCreate">0</Option>
      </Permission>
      </Permissions>
      <SpeedLimits ServerUlLimitBypass="0" UlLimit="10" UlType="0" ServerDlLimitBypass="0" DlLimit="10" DlType="0">
        <Download />
        <Upload />
      </SpeedLimits>
    </User>
  </Users>
  <Settings>
    <Item name="Serverports" type="string">21</Item>
    <Item name="Number of Threads" type="numeric">2</Item>
  <SpeedLimits>
     <Download />
     <Upload />
   </SpeedLimits>
 </Settings>

我正在尝试找到User Name =“3-Private”,然后删除整个节点。 我尝试过SelectNodes和SelectSingleNode。没有运气。

下面的2组代码。

我的控制台应用代码是:

   ' ---->  Using SelectNodes:

    Dim nodes As XmlNodeList
    Dim myXmlDocument As New XmlDocument()
    Dim strDeleteNode As String
    Dim strId As String
    Dim bSucess As Boolean = False

    strId = "3"
    strId = strId & "-Private"
    strDeleteNode = "/Users/User[@Name='" & strId & "']"

    ' The XmlDocument class represents the XML document and has a Load method to load the document from a file, stream, or an XmlReader.
    ' So load in the XML file.
    myXmlDocument.Load("MyFileZillafordeleting.xml")



    ' ----> My attempt at searching. It comes back with a zero count.


    nodes = myXmlDocument.SelectNodes(strDeleteNode)

    If nodes.Count = 0 Then
        Console.WriteLine("No nodes found.")
    Else
        ' Iterate through the children of the document element, and find the node I'm interested in, delete it.
        For Each node As XmlNode In nodes
            If node IsNot Nothing Then
                ' Removes all the children nodes but NOT the root.
                'node.RemoveAll()
                ' Removes all the children nodes and the root.
                node.ParentNode.RemoveChild(node)
                ' Use the Save method of the XmlDocument class to save the altered XML back to the input XML file.
                myXmlDocument.Save("MyFileZillafordeleting.xml")
                bSucess = True
            End If
        Next

        If bSucess = True Then
            Console.WriteLine("The XML file was saved successfully.")
        Else
            Console.WriteLine("The entry was not found.")
        End If
    End If



   ' ---->  Using SelectSingleNode:

    Dim node As XmlNode
    Dim myXmlDocument As New XmlDocument()
    Dim strDeleteNode As String
    Dim strId As String
    Dim bSucess As Boolean = False

    strId = "3"
    strId = strId & "-Private"
    strDeleteNode = "/Users/User[@Name='" & strId & "']"

    ' The XmlDocument class represents the XML document and has a Load method to load the document from a file, stream, or an XmlReader.
    ' So load in the XML file.
    myXmlDocument.Load("MyFileZillafordeleting.xml")

    ' ----> My attempt at searching. It comes back with "Nothing".

    node = myXmlDocument.SelectSingleNode(strDeleteNode)
    node.ParentNode.RemoveChild(node)

    myXmlDocument.Save("MyFileZillafordeleting.xml")

    Console.WriteLine("The XML file was saved successfully.")

...问候

1 个答案:

答案 0 :(得分:0)

您需要在开头更改使用//的路径,以确保在任何级别向下搜索,或者您需要使用精确,完整的路径,例如/FileZillaServer/Users/User[@Name='" & strId & "']"。以下是//的示例:

Dim node As XmlNode
Dim myXmlDocument As New XmlDocument()
Dim strDeleteNode As String
Dim strId As String
Dim bSucess As Boolean = False

strId = "3"
strId = strId & "-Private"
strDeleteNode = "//Users/User[@Name='" & strId & "']"

' The XmlDocument class represents the XML document and has a Load method to load the document from a file, stream, or an XmlReader.
' So load in the XML file.
myXmlDocument.Load("MyFileZillafordeleting.xml")

If node IsNot Nothing Then

node = myXmlDocument.SelectSingleNode(strDeleteNode)
node.ParentNode.RemoveChild(node)

myXmlDocument.Save("MyFileZillafordeleting.xml")

Console.WriteLine("The XML file was saved successfully.")
End If