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 237201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:24:23+00:00 2026-05-11T20:24:23+00:00

I am trying to load an XML file into a tree view control, edit

  • 0

I am trying to load an XML file into a tree view control, edit it and save it back in a different XML format.

<MyConfig>
  <description>
    <![CDATA[Add All config data]]>
  </description>
  <group name="Server">
    <description>
      <![CDATA[Server info]]>
    </description>
    <parameter name="Host" type="string">
      <description>
        <![CDATA[Host Name]]>
      </description>
      <value>cccc.ac.lk</value>
    </parameter>
    <parameter name="Port" type="integer">
      <description>
        <![CDATA[port no]]>
      </description>
      <range>0-65536</range>
      <value>47110</value>
    </parameter>
  </group>
</MyConfig>

I am using the following method to load the XML data

private void populateTreeControl(System.Xml.XmlNode document, 
System.Windows.Forms.TreeNodeCollection nodes)
{
  foreach (System.Xml.XmlNode node in document.ChildNodes)
  {
    string text = (node.Value != null ? node.Value : 
      (node.Attributes != null && node.Attributes.Count > 0) ? 
        node.Attributes[0].Value : node.Name);
    TreeNode new_child = new TreeNode(text);
    nodes.Add(new_child);
    populateTreeControl(node, new_child.Nodes);
  }
}

Now I want to filter out some nodes and load it to tree view. As an example it is useless for me to load description tags etc in the above case. I just want to create the tree with MyConfig –> Group –> Server—> Host and MyConfig –> Group –> Server—> Port

How should I modify the populateTreeControl() method to gain this?

  • 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-05-11T20:24:23+00:00Added an answer on May 11, 2026 at 8:24 pm
    private void populateTreeControl(
      System.Xml.Node context, 
      System.Windows.Forms.TreeNodeCollection treeNodes,
      List<string> xpath,
      int depth
    )
    {
      if (xpath.Count > depth) {
        foreach (System.Xml.XmlNode xmlNode in context.SelectNodes(xpath[depth]))
        {
          string text = "";
          if (xmlNode.Value != null) 
            text = xmlNode.Value;
          else if (xmlNode.Attributes.Count > 0)
            text = xmlNode.Attributes[0].Value;
          else 
            text = xmlNode.Name;
    
          TreeNode new_child = new TreeNode(text);
          treeNodes.Add(new_child);
    
          populateTreeControl(xmlNode, new_child.Nodes, xpath, depth + 1);
        }
      }
    }
    

    call as

    List<string> xpath = new List<string>();
    xpath.Add("/MyConfig/group/[@name='Server']");
    xpath.Add("parameter[@name='Host' or @name='Port']");
    
    populateTreeControl(xmlDoc, tree.Nodes, xpath, 0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Ah, yes, Word and it's lovely "smart quotes" feature. Have… May 11, 2026 at 11:47 pm
  • Editorial Team
    Editorial Team added an answer I don't think you can. Check the Apple's documentation for… May 11, 2026 at 11:47 pm
  • Editorial Team
    Editorial Team added an answer On Stackoverflow (and all Stack Exchange sites), a sitemap.xml file… May 11, 2026 at 11:47 pm

Related Questions

Trying to parse an HTML document and extract some elements (any links to text
I am trying to write a wrapper around a legacy COM object and install
I might be missing something obvious but is there a reference somewhere about what
I've been trying to figure out how to use an XML Schema to validate

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.