I have a treeview on my for and I need to write all the nodes, parent,
child to an XML file, How can i do that?
Write a recursive function? Here's an example:
// read bookmarks
XmlTextReader xr=new
XmlTextReader(M ainForm.AppPath ()+"\\bookmarks .xml");
xr.Read();
TreeNode n=BookmarksForm .tvwbm.Nodes[0];
ReadBookmarkXML (ref xr,n);
xr.Close();
// write bookmarks
XmlTextWriter xr=new
XmlTextWriter(M ainForm.AppPath ()+"\\bookmarks .xml",System.Te xt.Encoding.UTF 8);
xr.Formatting=F ormatting.Inden ted;
xr.Indentation= 4;
xr.WriteStartDo cument();
WriteBookmarkXM L(ref xr,BookmarksFor m.tvwbm.Nodes[0]);
xr.WriteEndDocu ment();
xr.Flush();
xr.Close();
private static void ReadBookmarkXML (ref XmlTextReader xr, TreeNode n)
{
while (!xr.EOF)
{
try
{
xr.ReadStartEle ment("Node");
TreeNode m=new TreeNode(xr.Rea dElementString( "NodeName") );
if (haveReadRootBo okmark)
{
n.Nodes.Add(m);
n=m;
}
else
{
haveReadRootBoo kmark=true;
}
BookmarksForm.h ashRef[n.GetHashCode()]=xr.ReadElement String("Ref");
BookmarksForm.h ashText[n.GetHashCode()]=xr.ReadElement String("Text");
}
catch
{
//no more sub-nodes
xr.ReadEndEleme nt();
n=n.Parent;
}
}
}
private static void WriteBookmarkXM L(ref XmlTextWriter xr, TreeNode n)
{
xr.WriteStartEl ement("Node");
xr.WriteElement String("NodeNam e",n.Text);
xr.WriteElement String("Ref",(s tring)
BookmarksForm.h ashRef[n.GetHashCode()]);
xr.WriteElement String("Text",( string)
BookmarksForm.h ashText[n.GetHashCode()]);
foreach (TreeNode m in n.Nodes)
{
WriteBookmarkXM L(ref xr,m);
}
xr.WriteEndElem ent();
}