Loading...

Populate TreeView Menu with XML

View: 235    Dowload: 1   Comment: 0   Post by: admin   Category: Visual studio & .NET   Fields: none

Download  populate-treeview-menu-with.rar (24.08 KB)

You need to Sign In to download the file populate-treeview-menu-with.rar
If you do not have an account then Sign up for free here

Introduction

Creating treeview menus on the fly from an XML file can be useful when the menu items are constantly being updated. For instance, when using an XML file as a database to store records in.

Here is a simple example of how to do this. The example is kept simple to avoid any confusion.

Background

A working knowledge of XML, the TreeView control, and the Visual Studio is helpful in understanding the steps.

Using the code

Note: The attached solution file (xml2treeviewmenuSolution) was created with VS2010.

Add an XML file to your project and name it "menu.xml". Edit the XML file with the menu items.

<?xml version="1.0" encoding="utf-8" ?>
<root>
<folder title='folder 1a' >
<record title='record 1a1' />
<record title='record 1a2' />
<folder title='folder 1b'>
<record title='record 1b1' />
</folder>
</folder>
<folder title='folder 2a' >
<record title='record 2a1' />
</folder>
<folder title='folder 3a' >
<record title='record 3a1' />
<record title='record 3a2' />
</folder>
</root>

Drag the TreeView control from the Visual Studio Toolbox onto your Windows form. In this example, I named the control "treeViewMenu".

Add references to the XML classes in your using statements.

[__strong__]using System.Xml;
using System.Xml.XPath; 

Create an XML document to hold the file.

public partial class Form1 : Form
{
    private XmlDocument docXML = new XmlDocument();

When the form is loaded, load the XML document with the XML file and begin populating the TreeView control.

private void Form1_Load(object sender, EventArgs e)
{
    docXML.Load("menu.xml"); // Load the xml file
    populateBaseNodes(); // Populate all of the base nodes
}

Population begins with the first level  nodes. After each base node is added to the tree, the child nodes for the current base node are added.

private void populateBaseNodes()
{
    treeViewMenu.Nodes.Clear(); // Clear any existing items
    treeViewMenu.BeginUpdate(); // Begin updating the treeview
    TreeNode treenode;
    treenode = treeViewMenu.Nodes.Add("Folders");
    
    XmlNodeList baseNodeList = docXML.SelectNodes("root/folder");
    // Get all first level  nodes

    foreach (XmlNode xmlnode in baseNodeList)
    // loop through all base  nodes 
    {
        string title = xmlnode.Attributes["title"].Value;

        treenode = treeViewMenu.Nodes.Add(title); // add it to the tree

        populateChildNodes(xmlnode, treenode); // Get the children
    }

    treeViewMenu.EndUpdate(); // Stop updating the tree
    treeViewMenu.Refresh(); // refresh the treeview display
}

Each child node will be inspected for further children. The loop will called for each child node that was found.

private void populateChildNodes(XmlNode oldXmlnode, TreeNode oldTreenode)
{
    TreeNode treenode = null;
    XmlNodeList childNodeList = oldXmlnode.ChildNodes;
    // Get all children for the past node (parent)

    foreach (XmlNode xmlnode in childNodeList)
    // loop through all children
    {
        string title = xmlnode.Attributes["title"].Value;
        // add it to the parent node tree
        treenode = oldTreenode.Nodes.Add(title);
        populateChildNodes(xmlnode, treenode); 
    }
}

Points of interest

Fancy up your menu using an image list and add an icon attribute to your menu file.

Populate TreeView Menu with XML

Creating treeview menus on the fly from an XML file can be useful when the menu items are constantly being updated. For instance, when using an XML file as a database to store records in.

Posted on 09-02-2018 

Comment:

To comment you must be logged in members.

Files with category

  • Pharmacy/Chemist POS and Invoicing Software

    Pharmacy/Chemist POS and Invoicing Software

    View: 144    Download: 14   Comment: 0

    Category: Visual studio & .NET     Fields: none

    Pharmacy POS /Chemist Management System allows User to sell medicine , Over the counter or at any Hospital, Easily Integrated with Email to SEND invoices and Print Receipt. This is one of my very best Project i Decided to Share with you Guys ...

  • Computer-Based Inventory Management System in VB.net

    Computer-Based Inventory Management System in VB.net

    View: 117    Download: 9   Comment: 0

    Category: Visual studio & .NET     Fields: none

    This is a project entitled Computer-Based Inventory Management System written in Vb.net and Microsoft access database for Surigao Doctors College. The system has the following features: - Cataloging - Inventory - Circulation - Borrow - Return - View...

  • Salary Management System

    Salary Management System

    View: 154    Download: 12   Comment: 0

    Category: Visual studio & .NET     Fields: none

    Introduction: ----------------- *This project is based on accounts concept. *Salary Management System is a DataBase system which can be used for managing employee Salary Details. *It is a multi-user system and can be used by hundreds of user at the...

  • New Print-maker Cyber Cafe

    New Print-maker Cyber Cafe

    View: 98    Download: 1   Comment: 0

    Category: Visual studio & .NET     Fields: none

    New Printmaker Cyber Cafe ------------------------- Before you run this project, please set the following date format “dd/MM/yyyy” to your computer. Introduction: -------------- The purpose of developing this software project is to fully automate...

  • Folder Locker 7.0

    Folder Locker 7.0

    View: 178    Download: 3   Comment: 0

    Category: Visual studio & .NET     Fields: none

    Folder Lock is great security app that lets you password-protect files, folders and drives; encrypt your important files on-the-fly, backup files in real-time, protect portable drives, shred files & drives and clean history. Folder Lock can...

  • Bakeshop Inventory System in VB.Net and MS Access Database

    Bakeshop Inventory System in VB.Net and MS Access Database

    View: 191    Download: 9   Comment: 0

    Category: Visual studio & .NET     Fields: none

    Bakeshop Inventory System is a software based system especially designed for a Bakeshop business and for the inventory process. This system will be able to track the daily, weekly, monthly and yearly inventory of products. In this way, the staff can...

  • Bezier Curve Machine Learning Demonstration

    Bezier Curve Machine Learning Demonstration

    View: 83    Download: 0   Comment: 0

    Category: Visual studio & .NET     Fields: Other

    I enjoy working with longitudinal data. This is the second article in a series about using Bezier curves to smooth large data point fluctuations and improve the visibility of the patterns unfolding. This current article focuses on using machine...

  • Basic Library Management System

    Basic Library Management System

    View: 148    Download: 4   Comment: 0

    Category: Visual studio & .NET     Fields: none

    Library System is designed to store students (borrowers) info and their individual file in an institution regarding on the library transactions. This system is a user-friendly program able to print reports, view students’ file or transactions in the...

 
File suggestion for you
Loading...
File top downloads
Loading...
Loading...
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd