Xml move nodes up and down-VBForums
Results 1 to 9 of 9

Thread: Xml move nodes up and down

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Posts
    85

    Xml move nodes up and down

    Hi

    I am tray to move nodes up and down
    I can move nodes down but I have problem move nodes up




    Code:
    
       Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
            SwapXNodes("Up", TextBox1.Text)   'treeview1.SelectedNode.Text
    
        End Sub
    
        Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
            SwapXNodes("down", TextBox1.Text)
        End Sub
    
    
    
    
           Dim xe As XElement
    
            xe = <TreeViewData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                     <Nodes>
                         <TreeNodeData>
                             <ID>4</ID>
                             <Text>Category 2</Text>
                             <Nodes>
                                 <TreeNodeData>
                                     <ID>5</ID>
                                     <Text>C2_material_1</Text>
                                 </TreeNodeData>
                                 <TreeNodeData>
                                     <ID>6</ID>
                                     <Text>C2_material_1</Text>
                                 </TreeNodeData>
                             </Nodes>
                         </TreeNodeData>
                         <TreeNodeData>
                             <ID>1</ID>
                             <Text>Category 1</Text>
                             <Nodes>
                                 <TreeNodeData>
                                     <ID>2</ID>
                                     <Text>C1_material_1</Text>
                                 </TreeNodeData>
                                 <TreeNodeData>
                                     <ID>3</ID>
                                     <Text>C1_material_1</Text>
                                 </TreeNodeData>
                             </Nodes>
                         </TreeNodeData>
                     </Nodes>
                 </TreeViewData>
    
            Dim currNode = (From c In xe...<Nodes>.<TreeNodeData> Where c.<Text>.Value = Text_string).FirstOrDefault
    
            If up = "up" Then
                If currNode.PreviousNode IsNot Nothing Then
                    Dim xPrevious As New XElement(DirectCast(currNode.PreviousNode, XElement))
                    ' copy of previous node
                    currNode.PreviousNode.ReplaceWith(currNode)
                    ' previous node equal to me
                    ' Now I should be equal to previous node
                    MsgBox(xPrevious.ToString)
                    currNode.ReplaceWith(xPrevious)
                End If
            Else
                If currNode.NextNode IsNot Nothing Then
                    Dim xNext As New XElement(DirectCast(currNode.NextNode, XElement))
                    ' copy of Next node
                    currNode.NextNode.ReplaceWith(currNode)
                    ' Next node equal to me
                    ' Now I should be equal to Next node copy
                    currNode.ReplaceWith(xNext)
                End If
            End If
    
            Debug.WriteLine(xe.ToString)

    Sample
    https://stackoverflow.com/questions/...es-up-and-down

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,425

    Re: Xml move nodes up and down

    At a cursory glance the code looks correct. Is it not working? If not, can you explain how it's not meeting your expectations?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  3. #3
    Lively Member
    Join Date
    May 2017
    Location
    Italy
    Posts
    68

    Re: Xml move nodes up and down

    I can not see SwapXNodes functions

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,425

    Re: Xml move nodes up and down

    Wait a minute.....does the code posted exactly look exactly like that in your application ? I assumed you just pasted it wrong in the post. If so, there's a lot more wrong there than just a couple missing functions. The code under Button10_Click should be inside a procedure and not just hanging there. Where did you even get that code ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Posts
    85

    Re: Xml move nodes up and down

    This is the code
    when I have only 2 nodes Category 1 and Category 2 then I can only move nodes down but if I have more Category then it work fine but not with the last Category can not move it up


    the SwapXNodes sample if from this url



    Code:
       Public Sub SwapXNodes(ByVal up_down As String, ByVal Text_string As String)
    
            Dim xe As XElement
    
            xe = <TreeViewData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                     <Nodes>
                         <TreeNodeData>
                             <ID>4</ID>
                             <Text>Category 2</Text>
                             <Nodes>
                                 <TreeNodeData>
                                     <ID>5</ID>
                                     <Text>C2_material_1</Text>
                                 </TreeNodeData>
                                 <TreeNodeData>
                                     <ID>6</ID>
                                     <Text>C2_material_1</Text>
                                 </TreeNodeData>
                             </Nodes>
                         </TreeNodeData>
                         <TreeNodeData>
                             <ID>1</ID>
                             <Text>Category 1</Text>
                             <Nodes>
                                 <TreeNodeData>
                                     <ID>2</ID>
                                     <Text>C1_material_1</Text>
                                 </TreeNodeData>
                                 <TreeNodeData>
                                     <ID>3</ID>
                                     <Text>C1_material_1</Text>
                                 </TreeNodeData>
                             </Nodes>
                         </TreeNodeData>
                     </Nodes>
                 </TreeViewData>
    
            Dim currNode = (From c In xe...<Nodes>.<TreeNodeData> Where c.<Text>.Value = Text_string).FirstOrDefault
    
            If up_down = "up" Then
                If currNode.PreviousNode IsNot Nothing Then
                    Dim xPrevious As New XElement(DirectCast(currNode.PreviousNode, XElement))
                    ' copy of previous node
                    currNode.PreviousNode.ReplaceWith(currNode)
                    ' previous node equal to me
                    ' Now I should be equal to previous node
                    MsgBox(xPrevious.ToString)
                    currNode.ReplaceWith(xPrevious)
                End If
            ElseIf up_down = "down" Then
                If currNode.NextNode IsNot Nothing Then
                    Dim xNext As New XElement(DirectCast(currNode.NextNode, XElement))
                    ' copy of Next node
                    currNode.NextNode.ReplaceWith(currNode)
                    ' Next node equal to me
                    ' Now I should be equal to Next node copy
                    currNode.ReplaceWith(xNext)
                End If
            End If
    
            Debug.WriteLine(xe.ToString)
    
        End Sub

  6. #6
    Lively Member
    Join Date
    May 2017
    Location
    Italy
    Posts
    68

    Re: Xml move nodes up and down

    how do you select the node to move ? from what to what ?

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Posts
    85

    Re: Xml move nodes up and down

    This is treeview sample in VB.NET Download

  8. #8
    Lively Member
    Join Date
    May 2017
    Location
    Italy
    Posts
    68

    Re: Xml move nodes up and down

    Your code runs well, I can move the last node Up. I'm using vs2017

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Posts
    85

    Re: Xml move nodes up and down

    Thanks patel45 then I need to check this better

    this is another sample it works well
    MoveElementup is sample from this Url


    Code:
           Dim elems = From el In xe...<Nodes>.<TreeNodeData> Where el.<Text>.FirstOrDefault.Value = TreeView1.SelectedNode.Text Select el 
            If elems.Count > 0 Then
                MoveElementup(elems(0))
            End If

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.