-
Nov 9th, 2017, 04:30 PM
#1
Thread Starter
Lively Member
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
-
Nov 11th, 2017, 02:40 AM
#2
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?
-
Nov 11th, 2017, 03:38 AM
#3
Addicted Member
Re: Xml move nodes up and down
I can not see SwapXNodes functions
-
Nov 11th, 2017, 09:34 AM
#4
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 ?
-
Nov 11th, 2017, 01:23 PM
#5
Thread Starter
Lively Member
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
-
Nov 11th, 2017, 03:18 PM
#6
Addicted Member
Re: Xml move nodes up and down
how do you select the node to move ? from what to what ?
-
Nov 11th, 2017, 04:59 PM
#7
Thread Starter
Lively Member
Re: Xml move nodes up and down
This is treeview sample in VB.NET Download
-
Nov 12th, 2017, 04:44 AM
#8
Addicted Member
Re: Xml move nodes up and down
Your code runs well, I can move the last node Up. I'm using vs2017
-
Nov 12th, 2017, 06:04 AM
#9
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|