Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML Treeview - XML Dom

Status
Not open for further replies.

colin81

Technical User
Jul 22, 2002
77
GB
Hi All

I wish to allow users to ammend a treeview (loaded with daa from an xml doc) which in turn ammends the xml document in the background i.e. ammend the xml dom then write to a file. Is this possible? I cannot seem to find anyway of indexing the selectednode to a node in the xml dom.

Many Thanks
Colin
 
I think there's a couple of ways to do this but can only remember one at the moment.

Try using the Treenode.Fullpath string as a reference to the respective node. You could set up a function that does a recursive loop through the xml document to match the node names with the treeview node names.

HTH
 
Thanks for the reply....that kinda solves my problem but if I have an xml file as below (I havent included doc prologs etc)

<test>
<anode>anode text</anode>
<aparentofnodes>
<node>some data</node>
<node>some mode data</node>
<aparentofnodes>
</test>

If I decide I want to remove the second <node> I cannot do this as it has the same path i.e. \test\anode\node

Is there anyway of finding an index or adding a hidden tag or id etc?

Thanks
Colin

 
that's a nice one...I've only worked with parents that had unique child nodes and didn't see that coming...sorry if I got you on the wrong track

how about this...it does seem a bit long winded and there's probably a quicker solution but it seems to work (will need some cleaning up)

all it's basically doing is getting the mapping of the tree node your deleting via the treenode index numbers and matching them to the child node in question

button2 loads the xml into the tree
button3 deletes the node

NOTE: in this case the treenode doesn't actually get removed...it just ceases to exist when the xml doc gets reloaded


Please let me know if you find something better or this doesn't work properly as I'm sure I'll need this or something like it in the future


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

PopulateTree()

End Sub

Private Function PopulateTree() As Boolean
m_oDoc.Load("C:\Documents and Settings\Hugh\My Documents\Visual Studio Projects\junk56\xmlfile1.xml")

Dim l_oNode As Xml.XmlNode
Dim l_oTreeNode As TreeNode

For Each l_oNode In m_oDoc.DocumentElement.ChildNodes
l_oTreeNode = New TreeNode(l_oNode.Name & l_oNode.Value)
tvw1.Nodes.Add(l_oTreeNode)
AddChildren(l_oTreeNode, l_oNode)
Next
End Function

Private Function AddChildren(ByRef r_oTreeNode As TreeNode, ByVal v_oXMLNode As Xml.XmlNode) As Boolean
Dim l_oXMLNode As Xml.XmlNode
Dim l_oTreeNode As TreeNode
Dim l_iIndex As Integer

For Each l_oXMLNode In v_oXMLNode.ChildNodes
If l_oXMLNode.Name <> "#text" Then
l_oTreeNode = New TreeNode(l_oXMLNode.Name & l_oXMLNode.InnerText)
r_oTreeNode.Nodes.Add(l_oTreeNode)

AddChildren(l_oTreeNode, l_oXMLNode)
l_iIndex += 1
End If

Next
End Function


Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

'get mapping
Dim l_sMapping As String = tvw1.SelectedNode.Index
If tvw1.SelectedNode.Parent Is Nothing Then

Else
GetMappingFromParents(tvw1.SelectedNode.Parent, l_sMapping)
End If

RemoveXMLNode(l_sMapping)

End Sub

Private Function GetMappingFromParents(ByVal v_oTreeNode As TreeNode, ByRef r_sMapping As String) As Boolean
r_sMapping = v_oTreeNode.Index & "," & r_sMapping
If v_oTreeNode.Parent Is Nothing Then

Else
GetMappingFromParents(v_oTreeNode.Parent, r_sMapping)
End If
End Function

Private Function RemoveXMLNode(ByVal v_sMapping As String) As Boolean
Dim l_sArray() As String = v_sMapping.Split(",")
Dim l_iLoop As Integer
Dim l_oXMLNode As Xml.XmlNode
Dim l_iCount As Integer
Dim l_oXMLNodeToDelete As Xml.XmlNode

For l_iLoop = 0 To l_sArray.Length - 1
'get nth node

Next

For Each l_oXMLNode In m_oDoc.DocumentElement.ChildNodes
If l_iCount = l_sArray(0) Then
'mapping match...continue
If l_sArray.Length > 1 Then
l_oXMLNodeToDelete = GetMappingRecurs(1, l_sArray, l_oXMLNode)
Else
l_oXMLNodeToDelete = l_oXMLNode
End If
End If
l_iCount += 1
Next

Dim l_oParentNode As Xml.XmlNode = l_oXMLNodeToDelete.ParentNode

l_oParentNode.RemoveChild(l_oXMLNodeToDelete)
m_oDoc.Save("C:\Documents and Settings\Hugh\My Documents\Visual Studio Projects\junk56\xmlfile1.xml")
tvw1.Nodes.Clear()
PopulateTree()

End Function

Private Function GetMappingRecurs(ByVal v_iIndex As Integer, ByVal v_sMappingArray() As String, ByVal v_oXMLNode As Xml.XmlNode) As Xml.XmlNode
Dim l_oXMLNode As Xml.XmlNode
Dim l_iCount As Integer

For Each l_oXMLNode In v_oXMLNode.ChildNodes
If l_iCount = v_sMappingArray(v_iIndex) Then
'mapping match...continue
If v_sMappingArray.Length > v_iIndex + 1 Then
GetMappingRecurs = GetMappingRecurs(v_iIndex + 1, v_sMappingArray, l_oXMLNode)
Else
GetMappingRecurs = l_oXMLNode
End If
End If
l_iCount += 1
Next
End Function
 
In your xpath, you use the indexer to select which one of several identical nodes you want to remove:
Code:
test/anode/aparentofnodes/node[1]
will select the 2nd node (it's 0-based)

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
jubble : thanks for the code I will give it a try and see how it goes.

chiph : I know its possible to select a node using the index in xpath in the xml dom, the problem Im having is mapping from the treeview to the dom...do you know a way of finding the current index of the treenode selected in a treeview?

Thanks both for replies
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top