2014-07-15 3 views
0

특정 요소를 찾기 위해 XML 문서를 트래핑하는 데 문제가있어서 전체 노드를 삭제할 수 있습니다. 같은 모양XmlDocument를 사용하여 요소를 찾은 다음 전체 노드를 삭제하는 방법은 무엇입니까?

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

내가 "3 개인"사용자 이름 =을 찾기 위해 노력하고 전체 노드를 삭제하고 있습니다. SelectSingleNode뿐만 아니라 SelectNodes를 시도했습니다. 불운.

아래 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.") 

....

답변

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

감사합니다. – user3020047

관련 문제