Transfer node from a treeView to another treeview in c#



Is very important , am working for an projet and want to trasnfert node from a treeview to another ( XML node ) . First , I browse my XML file in a treeview then I do the same with the xml file destination. ( Winforms). I select a node then I want to move it to the other tree. my XML file is like this one :



<obje>
<category title="B">
<category_value title="C0">
<object id="1000" name="DM_AAAA">
<child id="3795" x="0" y="0"/>
<child id="3798" x="0" y="238"/>
<child id="13794" x="0" y="44"/>

</object>
<object background_colour="230" id="1004" name="DM_PPPP">
<child id="3795" x="0" y="0"/>
<child id="3798" x="0" y="238"/>
<child id="13794" x="0" y="44"/>
<child id="13794" x="239" y="44"/>

</object>

</category_value>
</category>
<category title="Ma">
<category_value title="C0">
</object id="1211" name="DM_Reg" >


<object id="5063" name="SK" >
<child id="14998" x="0" y="0"/>
<child id="20955" x="0" y="0"/>
</object>

</category_value>
</category>
</obje>


then , My code is :



private void TransfertVersFichier2_Click(object sender, EventArgs e)
{
AllCheckedNodes.Clear();
if (GetAllCheckedNodes(treeView1.Nodes).Count == 0)
{
MessageBox.Show("Veuillez séléctionner un node à déplacer");
}
else if (GetAllCheckedNodes(treeView1.Nodes).Count == 1)
{
TreeNode nodeToTransfer = GetAllCheckedNodes(treeView1.Nodes)[0];
string nodeToTransferTagName = nodeToTransfer.Name;
string checkedId = nodeToTransfer.Tag.ToString();


var r_nodes = doc.GetElementsByTagName(nodeToTransferTagName);
XmlNode xmlNodeToTransfer = null;
foreach (XmlNode r_node in r_nodes)
{

if (r_node.Attributes != null && r_node.Attributes["id"] != null && r_node.Attributes["id"].Value == checkedId)
{
xmlNodeToTransfer = r_node;
}
else if (r_node.Attributes != null && r_node.Attributes["title"] != null && r_node.Attributes["title"].Value == checkedId)
{
xmlNodeToTransfer = r_node;
}
else if (r_node.Attributes != null && r_node.Attributes["name"] != null && r_node.Attributes["name"].Value == checkedId)
{
xmlNodeToTransfer = r_node;
}
}

if (xmlNodeToTransfer != null)
{
if (xmlNodeToTransfer.ParentNode.Attributes != null && xmlNodeToTransfer.ParentNode.Attributes["id"] != null)
{
/*TreeNode[] treeNodes_ch = treeView2.Nodes.Find(xmlNodeToTransfer.ParentNode.Name, true);
foreach (TreeNode ch_node in treeNodes_ch)
{
if (ch_node.Tag.ToString() == xmlNodeToTransfer.ParentNode.Attributes["id"].Value)
{
ch_node.Nodes.Insert(0, nodeToTransfer);
}
}*/

var tr_nodes = doc2.GetElementsByTagName(nodeToTransfer.Parent.Name);
foreach (XmlNode nodeToTransferParent in tr_nodes)
{
if (nodeToTransferParent.Attributes != null && nodeToTransferParent.Attributes["id"] != null && nodeToTransferParent.Attributes["id"].Value == xmlNodeToTransfer.ParentNode.Attributes["id"].Value)
{
XmlNode xml_nd = xmlNodeToTransfer.Clone();
nodeToTransferParent.AppendChild(xml_nd);
}
}
doc2.Save(Path2);
}
else if (xmlNodeToTransfer.ParentNode.Attributes != null && xmlNodeToTransfer.ParentNode.Attributes["title"] != null)
{
MessageBox.Show(xmlNodeToTransfer.ParentNode.Attributes["title"].Value);
}
else if (xmlNodeToTransfer.ParentNode.Attributes != null && xmlNodeToTransfer.ParentNode.Attributes["name"] != null)
{
MessageBox.Show(xmlNodeToTransfer.ParentNode.Attributes["name"].Value);
}
}

}
else
{
MessageBox.Show("Veuillez séléctionner un seul node àdéplacer");
}
}

No comments:

Post a Comment