2013-10-03 4 views
0

TreeView에서 일부 노드의 이름을 유지하는이 XML을 가지고 있습니다. TreeView에서 노드를 삭제 한 후에도 XML 파일에서 노드를 삭제해야합니다. 노드 프로파일 2의 내용을 삭제하는 코드를 실행했지만 부모 노드 인 "<Profile></Profile>"도 삭제하고 싶습니다.XML 노드 삭제 코드

올바른 코드를 알려주세요. 결과 코드를 실행 한 후

<?xml version="1.0" encoding="utf-8"?> 
<Profiles> 
    <Profile> 
    <Profile_Name>Profile 1</Profile_Name> 
    <Profile_Path>X:\Tests</Profile_Path> 
    </Profile> 
    <Profile> 
    <Profile_Name>Profile 2</Profile_Name> 
    <Profile_Path>X:\Tests</Profile_Path> 
    </Profile> 
</Profiles> 

:

은 원래 노드입니다

<?xml version="1.0" encoding="utf-8"?> 
<Profiles> 
    <Profile> 
    <Profile_Name>Prof 1</Profile_Name> 
    <Profile_Path>X:\Tests</Profile_Path> 
    </Profile> 
    <Profile> 
    </Profile> 
</Profiles> 

그리고 이것은 사용되는 코드입니다 :

Public Sub DeleteXml() 
    ProfileList.Load(xml_path) 
    Dim nodes_list As XmlNodeList = ProfileList.SelectNodes("Profiles") 
    Dim profile_node As XmlNode = ProfileList.SelectSingleNode("Profile") 
    Dim profile_name_node As XmlNode = ProfileList.SelectSingleNode("Profile_Name") 
    Dim bool As Boolean 

For Each profile_node In nodes_list 
      For Each profile_name_node In profile_node 
       If EManager.TreeView1.SelectedNode.Text = profile_name_node.FirstChild.InnerText Then 
        bool = True      
       Else 
        bool = False 
       End If 
       If bool = True Then 
        profile_name_node.RemoveAll() 
       End If 
      Next 
     Next 
    End Sub 

답변

0

이것은 가장 우아한 코드가 아닐지 모르지만 방금 테스트 한 결과 제대로 작동합니다.

Public Sub DeleteXml() 
    Dim profileList As New XmlDocument() 
    profileList.Load("XmlFile1.xml") 
    Dim profilesNode As XmlNode = profileList.SelectSingleNode("Profiles") 
    Dim profiles As XmlNodeList = profilesNode.SelectNodes("Profile") 
    For index As Integer = 0 To profiles.Count - 1 
     Dim profile As XmlNode = profiles(index) 
     If "Profile 2" = profile.FirstChild.InnerText Then 
      profile.ParentNode.RemoveChild(profile) 
     End If 
    Next 
    profileList.Save("XmlFile2.xml") 
End Sub 
+0

작동 중입니다. 고맙습니다! – guest

+0

그것을 듣기 좋게 Alex. 제발 [내 대답을 수락] (http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). – nunzabar