question

LooBi avatar image
0 Votes"
LooBi asked LooBi edited

Drag and Drop XML file to Treeview?

If I drag a xml file and drop it on the treeview, can a xml file be displayed? like this
Thank you !

196435-image.png


dotnet-csharpwindows-forms
image.png (1.3 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@LooBi, is any update? Do you agree with my answer?

0 Votes 0 ·
LooBi avatar image LooBi JackJJun-MSFT ·

@JackJJun-MSFT Ah Thank you! I forgot to accept. Thank you very much.

0 Votes 0 ·

1 Answer

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered

@LooBi, Welcome to Microsoft Q&A, you could use DragOver event and DragDrop event to show the xml in the treeview when you drop the xml file to the treeview control.

Here is a code example you could refer to.


  public Form1()
     {
         InitializeComponent();
         treeView1.AllowDrop = true;
     }
    
     private void treeView1_DragDrop(object sender, DragEventArgs e)
     {
         string filePath=string.Empty;
         if (e.Data.GetDataPresent(DataFormats.FileDrop))
         {
             string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
             filePath = files[0];
         }
         XmlDocument xDoc = new XmlDocument();
         xDoc.Load(filePath);
         treeView1.Nodes.Clear();
         treeView1.Nodes.Add(new TreeNode(xDoc.DocumentElement.Name));
         TreeNode tNode = new TreeNode();
         tNode = (TreeNode)treeView1.Nodes[0];
         addTreeNode(xDoc.DocumentElement, tNode);
         treeView1.ExpandAll();
            
     }
    
    
     private void addTreeNode(XmlNode xmlNode, TreeNode treeNode)
     {
         XmlNode xNode;
         TreeNode tNode;
         XmlNodeList xNodeList;
         if (xmlNode.HasChildNodes)
         {
             xNodeList = xmlNode.ChildNodes;
             for (int x = 0; x <= xNodeList.Count - 1; x++)
             {
                 xNode = xmlNode.ChildNodes[x];
                 string nodetext = xNode.Name + ": ";
                 Console.WriteLine(nodetext);
                 if (!nodetext.Contains("text"))
                 {
                     if (xNode.Attributes.Count > 0)
                     {
                         for (int i = 0; i < xNode.Attributes.Count; i++)
                         {
                             nodetext += xNode.Attributes[i].Name + " - " + xNode.Attributes[i].Value;
                         }
                     }
                 }
    
    
    
                 treeNode.Nodes.Add(new TreeNode(nodetext));
                 tNode = treeNode.Nodes[x];
                 addTreeNode(xNode, tNode);
             }
         }
         else
         {
             string nodetext = xmlNode.Name + ": ";
             Console.WriteLine(nodetext);
             if (!nodetext.Contains("text"))
             {
                 if (xmlNode.Attributes.Count > 0)
                 {
                     for (int i = 0; i < xmlNode.Attributes.Count; i++)
                     {
                         nodetext += xmlNode.Attributes[i].Name + " - " + xmlNode.Attributes[i].Value;
                     }
                 }
             }
             else
             {
                 nodetext = xmlNode.InnerText;
             }
             treeNode.Text = nodetext;
         }
    
     }
     private void treeView1_DragOver(object sender, DragEventArgs e)
     {
         if (e.Data.GetDataPresent(DataFormats.FileDrop))
             e.Effect = DragDropEffects.Link;
         else
             e.Effect = DragDropEffects.None;
     }

Result:

196310-1.gif



Best Regards,
Jack


If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



1.gif (47.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.