Home Forums WinForms controls Other WinForms controls Dynamically loading a TreeView

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #17970 |

    Hi all!

    I am using smartui treeview control in one of my form and i have a xml file. I want create TreeView follow tags structure of xml file.
    Help me! thanks very much.

    Imported from legacy forums. Posted by khanhnd (had 2118 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi Khanhnd,

    The solution I use is quite extended, so I will jusy show you a few of the essentials.
    First you need to load the xml file:

    /// <summary>
    /// Set the data source for the command data
    /// </summary>
    /// <param name=”decoder”></param>
    /// <param name=”name”></param>
    public void Load()
    {
    this._data = ( ViewData )MultiSerializer.LoadXML( this._fileInfo, typeof( ViewData ));
    if( this._data == null )
    this._data = new ViewData();
    if( CommandsLoaded != null )
    CommandsLoaded( this, new EventArgs() );
    }

    The “multiserializer” is a wrapper around ‘normal’ xml formatting,SOAP formatting and binary formatting:

    using System.Xml;
    using System.Xml.Serialization;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Soap;
    using System.Runtime.Serialization.Formatters.Binary;

    /// <summary>
    /// Load the data from the file with the given name
    /// </summary>
    /// <param name=”fileInfo”></param>
    /// <returns></returns>
    public static object Load( FileInfo fileInfo )
    {
    object data = null;
    FileStream fs = null;
    // First try soap
    try
    {
    SoapFormatter formatter = new SoapFormatter();
    fs = new FileStream( fileInfo.FullName, FileMode.Open, ileAccess.Read, FileShare.Read);
    data = formatter.Deserialize( fs );
    }
    catch { /* ignore */ }
    finally
    {
    if( fs != null )
    fs.Close();
    fs = null;
    }
    // Check if success with soap
    if (data != null)
    return data;

    // Now try binary
    try
    {
    BinaryFormatter formatter = new BinaryFormatter();
    fs = new FileStream( fileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.Read);
    data = formatter.Deserialize( fs );
    }
    catch (Exception e) { throw e; }
    finally
    {
    if( fs != null )
    fs.Close();
    fs = null;
    }
    return data;
    }
    }

    /// <summary>
    /// Returns a datastore object with the contents of the requested file name.
    /// The type of the stored object is required
    /// </summary>
    public static object LoadXML( FileInfo fileInfo, System.Type type )
    {
    object data = null;
    FileStream fs = null;
    try
    {
    XmlSerializer serializer = new XmlSerializer( type );
    fs = new FileStream( fileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.Read);
    data = serializer.Deserialize( fs );
    }
    catch( Exception ex )
    {
    throw ex;
    }
    finally
    {
    if( fs != null )
    {
    fs.Flush();
    fs.Close();
    }
    }
    return data;
    }

    The ViewData object is stored as xml structure. Saving is almost the same as loading (use serialise instead of deserialise)

    Now you need to recreate the tree structure using Xceed.Node

    /// <summary>
    /// Recursively expands the tree from the given node with the subtree
    /// and expands the nodes if requested
    /// </summary>
    /// <param name=”node”></param>
    /// <param name=”action”></param>
    /// <param name=”number”></param>
    /// <param name=”index”></param>
    /// <returns></returns>
    protected virtual void ExpandTree( ViewData root, Node node )
    {
    Node childNode;
    foreach( ViewData data in root.Children )
    {
    childNode = new Node()
    FillNode( data, childNode ); //Whatever needs to be shown
    this.ExpandTree( data, childNode );
    }
    }

    /// </summary>
    /// Make this available to the smarttreeview
    /// <param name=”node”></param>
    /// <param name=”action”></param>
    /// <param name=”number”></param>
    /// <param name=”index”></param>
    /// <returns></returns>
    public virtual Node ExpandTree( ViewData root )
    {
    Node rootNode = new Node();
    FillNode( root, rootNode );
    this.ExpandTree( data, childNode );
    return rootNode;
    }

    Last you can add this to the smarttreeview:

    smartTreeview.Items.Add( ExpandTree( viewData ));

    I know this is a bit fragementary, but I hope you can see the essence.

    Kees

    Imported from legacy forums. Posted by keesp (had 3126 views)

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.