Treeview storage and reading

Source: Internet
Author: User

Using system;
Using system. Windows. forms;
Using system. xml;
Using system. Globalization;

Namespace treesample
{
/// <Summary>
/// Summary description for treefunctions.
/// </Summary>
Public class treefunctions
{
Public treefunctions ()
{
// Add constructor logic here
}

Internal system. xml. xmldocument tree2xml (Ref system. Windows. Forms. Treeview otreeview)
{
System. Text. stringbuilder stroutput = new system. Text. stringbuilder ();
Stroutput. append ("<? XML version =/"1.0/"?> /N ");
Stroutput. append ("Inclutreenodes (otreeview. nodes, ref stroutput );
Stroutput. append ("

System. xml. xmldocument oxml = new system. xml. xmldocument ();
Try
{
Oxml. loadxml (stroutput. tostring ());
}
Catch (system. xml. xmlexception excxml)
{
MessageBox. Show ("cannot load XML ");
Application. Exit ();
}
Return oxml;
}

Private void implements treenodes (system. Windows. Forms. treenodecollection xnodecol, Ref system. Text. stringbuilder Strout)
{
Foreach (enhancedtreenode xnode in xnodecol)
{
Strout. append ("<node id =/" "+ xnode. itemid. tostring (cultureinfo. invariantculture) + "/" position =/"" + xnode. index + "/">/N ");
Strout. append ("<! [CDATA ["+ xnode. itemname +"]>/N ");
If (xnode. getnodecount (true)> 0)
{
Repeated treenodes (xnode. nodes, ref Strout );
}
Strout. append ("</node>/N ");
}
}

Internal void createtree (string strhierarchy, system. Windows. Forms. treenodecollection xtreenodecol)
{
System. xml. xmltextreader XTR = new system. xml. xmltextreader (
New system. Io. stringreader (strhierarchy ));
Int lastdepth =-1;
Int currentdepth =-1;
Enhancedtreenode xnode = NULL;
Treenodecollection xcol = NULL;
XTR. whitespacehandling = whitespacehandling. None;
While (XTR. Read ())
{
If (XTR. nodetype = xmlnodetype. element)
{
Switch (XTR. Name)
{
Case "Node ":
Xnode = new enhancedtreenode ();
Currentdepth = XTR. depth;
XTR. movetoattribute ("ID ");
XTR. readattributevalue ();
Xnode. Itemid = convert. toint32 (XTR. Value, cultureinfo. invariantculture );
XTR. movetoelement ();
XTR. movetoattribute ("inactive ");
XTR. readattributevalue ();
If (lastdepth =-1) // It's the root!
{
Xcol = xtreenodecol;
}
Else if (currentdepth <lastdepth) // move up a node
{
For (INT I = 0; I <lastdepth-currentdepth; I ++)
{
Xcol = xcol [xcol. Count-1]. Parent. Parent. nodes;
}
}
Else if (currentdepth = lastdepth) // stay in the same collection
{
Xcol = xcol;
}
Else if (currentdepth> lastdepth) // move down a node
{
For (INT I = 0; I <currentdepth-lastdepth; I ++)
{
Xcol = xcol [xcol. Count-1]. nodes;
}
}
XTR. movetoelement ();
Lastdepth = XTR. depth;
Break;
Case "name ":
Xnode. itemname = XTR. readstring ();
Xcol. Add (xnode );
Break;
Default:
Break;
}
}
}
XTR. Close ();
}
}

Public sealed class enhancedtreenode: system. Windows. Forms. treenode
{
Private int m_intitemid =-1;
Private string m_stritemname = string. empty;

Public enhancedtreenode ()
{
// Add constructor logic here
}

Public enhancedtreenode (string itemname, int Itemid)
{
M_intitemid = Itemid;
M_stritemname = itemname;
This. Text = itemname;
}

Public int Itemid
{
Get {return m_intitemid ;}
Set {m_intitemid = value ;}
}

Public String itemname
{
Get {return m_stritemname ;}
Set {m_stritemname = value; this. Text = m_stritemname ;}
}
}
}

 

// Read XML

Private void menuitem3_click (Object sender, system. eventargs E)
{
Dialogresult answer = openfiledialog1.showdialog ();
If (answer! = Dialogresult. Cancel)
{
System. Io. textreader TR = system. Io. file. opentext (openfiledialog1.filename );
Treefunctions TF = new treefunctions ();
String hierarchy = tr. readtoend ();
Tr. Close ();
TF. createtree (hierarchy, treeview1.nodes );
}
}

// Exit

Private void menuitem5_click (Object sender, system. eventargs E)
{
Application. Exit ();
}

// Save as an XML file

Private void menuitem4_click (Object sender, system. eventargs E)
{
Dialogresult answer = savefiledialog1.showdialog ();
If (answer! = Dialogresult. Cancel)
{
Treefunctions TF = new treefunctions ();
TF. tree2xml (ref treeview1). Save (savefiledialog1.filename );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.