How to edit treeview xml file and save in Winforms?

LooBi 141 Reputation points
2022-04-12T06:13:21.313+00:00

I'm trying to make a XML Editor.
My Code wroks. I choose a file and it is shown to Application.
How to edit a node by double-clicking it and save it by pressing Crtl+S?
Thank you for your help!

    private void button1_Click(object sender, EventArgs e)  
    {  
        OpenFileDialog dlg = new OpenFileDialog();  
        dlg.Title = "Open XML Document";  
        dlg.Filter = "XML Files (*.xml)|*.xml";  

        if (dlg.ShowDialog() == DialogResult.OK)  
        {  
            try  
            {  
                this.Cursor = Cursors.WaitCursor;  

                XmlDocument xDoc = new XmlDocument();  
                xDoc.Load(dlg.FileName);  
                tvLabel.Nodes.Clear();  
                tvLabel.Nodes.Add(new TreeNode(xDoc.DocumentElement.Name));  
                TreeNode tNode = new TreeNode();  
                tNode = (TreeNode)tvLabel.Nodes[0];  

                addTreeNode(xDoc.DocumentElement, tNode);  

                tvLabel.ExpandAll();  
            }  
            catch (Exception ex)  
            {  
                MessageBox.Show(ex.Message);  
            }  
            finally  
            {  
                this.Cursor = Cursors.Default;  
            }  
        }  
    }  

    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];  
                treeNode.Nodes.Add(new TreeNode(xNode.Name));  
                tNode = treeNode.Nodes[x];  
                addTreeNode(xNode, tNode);  
            }  
        }  
        else  
            treeNode.Text = xmlNode.OuterXml.Trim();  
    }  

192201-image.png

Developer technologies Windows Forms
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2022-04-13T02:14:10.24+00:00

    @LooBi ,Welcome to Microsoft Q&A, you could override ProcessCmdKey method and use NodeMouseDoubleClick event to get what you wanted.
    Here is a code example you could refer to.

    Code:

     public string FileName { get; set; }      
                private void button1_Click(object sender, EventArgs e)  
                {  
                    OpenFileDialog dlg = new OpenFileDialog();  
                    dlg.Title = "Open XML Document";  
                    dlg.Filter = "XML Files (*.xml)|*.xml";  
                    if (dlg.ShowDialog() == DialogResult.OK)  
                    {  
                        try  
                        {   
                            FileName = dlg.FileName;  
                            this.Cursor = Cursors.WaitCursor;  
                            XmlDocument xDoc = new XmlDocument();  
                            xDoc.Load(dlg.FileName);  
                            tvLabel.Nodes.Clear();  
                            tvLabel.Nodes.Add(new TreeNode(xDoc.DocumentElement.Name));  
                            TreeNode tNode = new TreeNode();  
                            tNode = (TreeNode)tvLabel.Nodes[0];  
                            addTreeNode(xDoc.DocumentElement, tNode);  
                            tvLabel.ExpandAll();  
                        }  
                        catch (Exception ex)  
                        {  
                            MessageBox.Show(ex.Message);  
                        }  
                        finally  
                        {  
                            this.Cursor = Cursors.Default;  
                        }  
                    }  
                }  
          
                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];  
                            treeNode.Nodes.Add(new TreeNode(xNode.Name));  
                            tNode = treeNode.Nodes[x];  
                            addTreeNode(xNode, tNode);  
                        }  
                    }  
                    else  
                        treeNode.Text = xmlNode.OuterXml.Trim();  
                }  
          
                private void tvLabel_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)  
                {  
                    tvLabel.LabelEdit = true;  
                    tvLabel.SelectedNode.BeginEdit();  
             
                  
                }  
          
                protected override bool ProcessCmdKey(ref Message msg, Keys keyData)  
                {  
                    if (keyData == (Keys.Control | Keys.S))  
                    {  
                        exportToXml(tvLabel, FileName);  
                        return true;  
                    }  
                    return base.ProcessCmdKey(ref msg, keyData);      
                }  
                StreamWriter sr = null;  
                public void exportToXml(TreeView tv, string filename)  
                {  
                    sr = new StreamWriter(filename, false, System.Text.Encoding.UTF8);  
                    sr.WriteLine("<" + tv.Nodes[0].Text + ">");  
                    foreach (TreeNode node in tv.Nodes)  
                    {  
                        saveNode(node.Nodes);  
                    }  
                    //Close the root node  
                    sr.WriteLine("</" + tv.Nodes[0].Text + ">");  
                    sr.Close();  
                }  
                private void saveNode(TreeNodeCollection tnc)  
                {  
                    foreach (TreeNode node in tnc)  
                    {  
          
                        if (node.Nodes.Count > 0)  
                        {  
                            sr.Write("<" + node.Text + ">");  
                            saveNode(node.Nodes);  
                            sr.WriteLine("</" + node.Text + ">");  
                        }  
                        else  
                            sr.Write(node.Text);  
                    }  
                }  
    

    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 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.