I use the following code to display xml as a treeview.
Now I want to refresh when I edit xml by using some text box in same window. When I edit and save file using textbox I want to refresh treeview also with that new value.
How can I do it?
I use treeview1.update(); and treeview1.refresh(); but it’s not working.
I used the following code part:
private void button1_Click(object sender, EventArgs e)
{
XmlDataDocument xmldoc = new XmlDataDocument();
XmlNode xmlnode ;
FileStream fs = new FileStream("tree.xml", FileMode.Open, FileAccess.Read);
xmldoc.Load(fs);
xmlnode = xmldoc.ChildNodes[1];
treeView1.Nodes.Clear();
treeView1.Nodes.Add(new TreeNode(xmldoc.DocumentElement.Name));
TreeNode tNode ;
tNode = treeView1.Nodes[0];
AddNode(xmlnode, tNode);
}
private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
{
XmlNode xNode ;
TreeNode tNode ;
XmlNodeList nodeList ;
int i = 0;
if (inXmlNode.HasChildNodes)
{
nodeList = inXmlNode.ChildNodes;
for (i = 0; i <= nodeList.Count - 1; i++)
{
xNode = inXmlNode.ChildNodes[i];
inTreeNode.Nodes.Add(new TreeNode(xNode.Name));
tNode = inTreeNode.Nodes[i];
AddNode(xNode, tNode);
}
}
else
{
inTreeNode.Text = inXmlNode.InnerText.ToString();
}
}
You can create the treeview completely new after saving your changes. In order to reuse your existing code, you could refactor it a bit:
…and after saving your modifications to the xml file, call
FillTreeView():