Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8084365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:46:27+00:00 2026-06-05T17:46:27+00:00

In my treeview I’m able to load xml data with images but this code

  • 0

In my treeview I’m able to load xml data with images but this code applies a common image to all the nodes.

How can i apply a specific image for Parent node, sub_child node..etc. For example, I want to assign a different image for countries, country, name, states, state with respect to the XML structure.

I have loaded 5 different images in the image directory. I need some example to add different images to my treeview.

private void populateTree()
{
    ImageList li = new ImageList();
    li.ImageSize = new Size(32, 32);
    li.ColorDepth = ColorDepth.Depth32Bit;
    treeView1.ImageList = li;

    System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(Application.StartupPath + @"\images");
    foreach (System.IO.FileInfo file in dir.GetFiles("*.*"))
    {
        li.Images.Add(file.Name, Image.FromFile(@"images\" + file.Name));
        treeView1.Nodes.Add(null, file.Name, file.Name.ToString(), file.Name.ToString());
    }

    //treeView1.ImageList = li;

    //treeView1.SelectedNode.ImageIndex = 0;
    //treeView1.SelectedNode.SelectedImageIndex = 1;

    var filename = @"C:\Countries.xml";
    //First, we'll load the Xml document
    XmlDocument xDoc = new XmlDocument();
    xDoc.Load(filename);

    //Now, clear out the treeview, and add the first (root) node
    treeView1.Nodes.Clear();
    treeView1.Nodes.Add(new TreeNode(xDoc.DocumentElement.Name));
    TreeNode tNode = new TreeNode();
    tNode = (TreeNode)treeView1.Nodes[0];
    //Here make a call to AddNode, where we'll add all of our nodes
    addTreeNode(xDoc.DocumentElement, tNode);

    //Expand the treeview to show all nodes
    treeView1.ExpandAll();



}


private void addTreeNode(XmlNode xmlNode, TreeNode treeNode)
{
    XmlNode xNode;
    TreeNode tNode;
    XmlNodeList xNodeList;

    if (xmlNode.HasChildNodes) //The current node has children
    {
        xNodeList = xmlNode.ChildNodes;

        for (int x = 0; x <= xNodeList.Count - 1; x++) //Loop through the child nodes
        {
            xNode = xmlNode.ChildNodes[x];
            treeNode.Nodes.Add(new TreeNode(xNode.Name));
            tNode = treeNode.Nodes[x];
            addTreeNode(xNode, tNode);
        }
    }
    else //No children, so add the outer xml (trimming off whitespace)
        treeNode.Text = xmlNode.OuterXml.Trim();
}

XML File:

<?xml version="1.0" encoding="utf-8" ?> 
<countries>
    <country>
        <name>India</name>
        <states>
            <state>TamilNadu</state>
            <state>Andhra</state>
            <state>Kerala</state>
            <state>Karnataka</state>
        </states>
    </country>
</countries>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:46:28+00:00Added an answer on June 5, 2026 at 5:46 pm

    I think you’re searching for TreeNode.ImageIndex property use.

    Assign an index of the Image present in TreeView‘s associated ImageList.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a treeview in my C# code. I want to replace all existing
this.treeView.Nodes.Add(node); bool my_node_exists = this.treeView.Nodes.Contains(node); TreeNode[] my_nodes = this.treeView.Nodes.Find(node.Text, true); Here, my_node_exists is true
I have a treeview with nodes like this: Foo (1234), and want to allow
My TreeView nodes have a checkbox. If the User checks one checkbox, all other
I have a TreeView that binds to XML data and one DataGrid that binds
My Treeview looks like this: its haml submission_database = @users.each do |user| %ul %li
I have a treeview with multiple nodes with the same text and quite nested
I have a TreeView databound to a CollectionViewSource Groups collection. This is so that
I have a TreeView with user-editable nodes. I like to use the node caption
I have a TreeView with some nodes inside of it. Whenever the user clicks

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.