Move xml childnodes between parentnodes?

Thierry

Member
Joined
May 15, 2008
Messages
7
Programming Experience
3-5
Hi, welcome (again). :rolleyes:


Let me first explain my situation. I need to create a program that can easly move childnodes between parents. So I created 2 listboxes: one is filled with all childnodes and one is filled with all parents.
When a childnode and parent is selected the move button should move the selected childnode to his new parent (see picture attachment).

Well everything works now and the only thing I have to create still is the move action. Any suggestions what the best way is to create this? :afrog:

Code I have now currently (dosn't work):
VB.NET:
        ' Actions behind the Move button

        Dim xmlDoc As New XmlDocument()
        xmlDoc.Load(Filepath)
        Dim SelectedNode As XmlNode = xmlDoc.DocumentElement
        XmlCN = LBUtterance.SelectedItem.ToString
        XmlPN = LBCategory.SelectedItem.ToString
        Dim xmlRoot As XmlNode

        MessageBox.Show(XmlPN + " + " + XmlCN)

        For Each SelectedNode In xmlDoc.DocumentElement

            If SelectedNode.Name = XmlPN AndAlso XmlCN <> "" Then

                MessageBox.Show("test!")

                xmlRoot = SelectedNode.Clone()
                Dim XmlE As XmlElement = xmlDoc.CreateElement("utterance")
                XmlE.InnerText = XmlCN

                xmlRoot.AppendChild(xmlRoot)
                xmlDoc.ReplaceChild(xmlRoot, SelectedNode)

                xmlDoc.Save("C:\test.xml")

            End If
        Next
XML:
HTML:
<?xml version="1.0"?>
<labels>
	<category name="afschrift">
		<utterance>voor afschriften</utterance>
		<utterance>afschrijvingsinformatie</utterance>
	</category>
	<category name="blabla">
		<utterance>opvragen</utterance>
		<utterance>bankafschriften</utterance>
	</category>
</labels>
Etc. etc. etc.

Thanks in advance,
Thista
 

Attachments

  • Program.gif
    Program.gif
    20.6 KB · Views: 21
You don't have to load the XmlDocument for each button click, declare it as a private class variable and only Load it on startup.

The code to move the utterance node to a category node is simple, select both nodes and use AppendChild method to add it to the new parent. Save the XmlDocument for changes to take effect in data file, this can be done on button click or when app closes.

How do you see in your view which category an utterance already belongs to? In below example I find the category and select it when a utterance node is selected so user can see. Different UI could give user a better view of the node tree.
VB.NET:
Private doc As New Xml.XmlDocument

Private Sub utt_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles Me.FormClosing
    doc.Save("utterance.xml")
End Sub

Private Sub utt_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
    doc.Load("utterance.xml")
    For Each cat As Xml.XmlNode In doc.SelectNodes("//category/@name")
        Me.CategoryListbox.Items.Add(cat.InnerText)
    Next
    For Each utt As Xml.XmlNode In doc.SelectNodes("//utterance")
        Me.UtteranceListbox.Items.Add(utt.InnerText)
    Next
End Sub

[COLOR="Green"]'set category for a utterance node[/COLOR]
Private Sub MoveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MoveButton.Click
    Dim xpath As String
    [COLOR="green"]'get category[/COLOR]
    xpath = String.Format("//category[@name='{0}']", Me.CategoryListbox.SelectedItem)
    Dim cat As Xml.XmlNode = doc.SelectSingleNode(xpath)
    [COLOR="green"]'get utterance[/COLOR]
    xpath = String.Format("//utterance[.='{0}']", Me.UtteranceListbox.SelectedItem)
    Dim utt As Xml.XmlNode = doc.SelectSingleNode(xpath)
    [COLOR="green"]'set parent[/COLOR]
    cat.AppendChild(utt)        
End Sub

[COLOR="green"]'mark which category a utterance node currently belongs to[/COLOR]
Private Sub UtteranceListbox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles UtteranceListbox.SelectedIndexChanged
    Dim xpath As String = String.Format("//category[utterance='{0}']/@name", Me.UtteranceListbox.SelectedItem)
    Dim cat As Xml.XmlNode = doc.SelectSingleNode(xpath)
    Me.CategoryListbox.SelectedItem = cat.InnerText
End Sub
 
No idea how to thank you but thanks allot..
Great! We currently have the forum reputation system, you can click the "scale" icon of each post to rate replies good/bad. Also top of thread you can click "Rate this thread".
 

Similar threads

Back
Top