Tal770

hi,

I'v looped on a treeview and stored all the nodes in List <TreeNode> collection

Code Snippet

private void GetNodes(TreeNodeCollection nodes, List<TreeNode> list)

{

foreach (TreeNode node in nodes)

{

list.Add(node);

GetNodes(node.Nodes, list);

}

}

suppose I add

root -- node0 -- node2 -- node5

| |

| |- node3 --node6

|

|- node1 -- node4

the List holds as follows

0 - root

1 - node0

2 - node2

3 - node5

4 - node3

5 - node6

6 - node1

7 - node4

1. I want to save it to disk should I save it using streamwriter

2. I then want to load this file on start of the program what load method should I use

3. then I want to populate the tree and here I'm stuck! I tried some code based on examples from the form:

Code Snippet

private void load_Click(object sender, EventArgs e)

{

treeView1.BeginUpdate();

treeView1.Nodes.Clear();

List<TreeNode> roots = new List<TreeNode>();

TreeNode tn = (TreeNode)nodeList[0];

treeView1.Nodes.Add(tn);

roots.Add(tn);

//the roots is used to store the current node and parent node

for (int i = 1; i < nodeList.Count; i++)

{

TreeNode item = nodeList[i];

if (item.Level == roots.Count) roots.Add(roots[roots.Count - 1].LastNode);

AddJob(roots[item.Level],item);

}

treeView1.ExpandAll();

treeView1.EndUpdate();

}

private void AddJob(TreeNode targetNode, TreeNode inputNode)

{

if (targetNode.LastNode == null)

{

targetNode.Nodes.Insert(targetNode.Index, inputNode);

}

else

{

targetNode.Nodes.Add(inputNode);

}

}

and the I get exception that Im tring to inser a node not in the right place what should I do whats wrong here



Re: Windows Forms General Saving and loding TreeView problem using List<TreeNode> collections

Bob zhu - MSFT

Hi I think you can save treeview content in XML files, follow is two Method

Code Snippet

private SaveFileDialog dlgSave=new SaveFileDialog();

private void button2_Click(object sender, EventArgs e)

{

if (this.dlgSave.ShowDialog() == DialogResult.OK)

{

XmlDocument doc = new XmlDocument();

doc.LoadXml("

"
);

XmlNode root = doc.DocumentElement;

doc.InsertBefore(doc.CreateXmlDeclaration("1.0", "utf-8", "yes"), root);

TreeNode2Xml(this.treeView1.Nodes, root);

doc.Save(dlgSave.FileName);

}

}

private void TreeNode2Xml(TreeNodeCollection treeNodes, XmlNode xmlNode)

{

XmlDocument doc = xmlNode.OwnerDocument;

foreach (TreeNode treeNode in treeNodes)

{

XmlNode element = doc.CreateNode("element", "Item", "");

XmlAttribute attr = doc.CreateAttribute("Title");

attr.Value = treeNode.Text;

element.Attributes.Append(attr);

element.AppendChild(doc.CreateCDataSection(treeNode.Text.ToString()));

xmlNode.AppendChild(element);

if (treeNode.Nodes.Count > 0)

{

TreeNode2Xml(treeNode.Nodes, element);

}

}

}

Code Snippet
#region XML 2 TreeView
private void btnLoad_Click(object sender, EventArgs e)
{
// XML中 取 据到TreeView
if (this.dlgOpen.ShowDialog() == DialogResult.OK)
{
XmlDocument xmlDoc
= new XmlDocument();
xmlDoc.Load(dlgOpen.FileName);

XmlNodeList xmlNodes
= xmlDoc.DocumentElement.ChildNodes;

this.treeView1.BeginUpdate();
this.treeView1.Nodes.Clear();
XmlNode2TreeNode(xmlNodes,
this.treeView1.Nodes);
this.treeView1.EndUpdate();
}
}

private void XmlNode2TreeNode(XmlNodeList xmlNode, TreeNodeCollection treeNode)
{
foreach (XmlNode var in xmlNode)
{
if (var.NodeType != XmlNodeType.Element)
{
continue;
}
TreeNode newTreeNode
= new TreeNode();
newTreeNode.Text
= var.Attributes["Title"].Value;

if (var.HasChildNodes)
{
if (var.ChildNodes[0].NodeType == XmlNodeType.CDATA)
{
newTreeNode.Tag
= var.ChildNodes[0].Value;
}

XmlNode2TreeNode(var.ChildNodes, newTreeNode.Nodes);
}
treeNode.Add(newTreeNode);
}
}
#endregion







Re: Windows Forms General Saving and loding TreeView problem using List<TreeNode> collections

Tal770

the code you post doesnt work:

Code Snippet

doc.LoadXml(" "); I dont have yet the XML file so why load

XmlNode root = doc.DocumentElement;

// root received NULL which is wrong.

the root receive null value and the function crash when givin null value.

I also have values that I store

1. In the treeNode.Tag I hold a unique key data that I created for each node

2. In the treeNode.ToolTipText I hold relevent text for each node

thouse data need to be stored and later to recreate the tree including them, probably I should use:

Code Snippet

XmlAttribute attr2 = doc.CreateAttribute("Tag");

attr2.Value = treeNode.Tag;

element.Attributes.Append(attr2);

XmlAttribute attr3= doc.CreateAttribute("ToolTips");

attr3.Value = treeNode.ToolTipsText;

element.Attributes.Append(attr3);

do I need to create for each one an attribute or I can inclued all three in to one

10X





Re: Windows Forms General Saving and loding TreeView problem using List<TreeNode> collections

Zhi-Xin Ye - MSFT

It's better to store and loading a TreeView through a XML file instead of List<TreeNode>, there're lots of projects on CodeProject for your information, go to http://www.codeproject.com/info/search.asp, and use this keyword "treeview xml" for searching.