0

I am new to c# and the problem i am facing is that i have an XML file which include a list of process information with fields process id, process name and parent id. i nedd help in populating this xml file into tree view. The XML file is not formatted one. My XML file will look like:

<Process id="0" name="[System Process]" ParentPID="0" />
<Process id="4" name="System" ParentPID="0" />
<Process id="100" name="MyApp.exe" ParentPID="4" />
<Process id="120" name="avgrsx.exe" ParentPID="10"/>
<Process id="150" name="avgcsrvx.exe" ParentPID="120" />
<Process id="155" name="csrss.exe" ParentPID="100" />
<Process id="170" name="winlogon.exe" ParentPID="100" />
<Process id="180" name="services.exe" ParentPID="170" />
<Process id="200" name="lsass.exe" ParentPID="170" />
<Process id="110" name="svchost.exe" ParentPID="180" />
<Process id="380" name="svchost.exe" ParentPID="200"/>
<Process id="530" name="svchost.exe" ParentPID="1764" />
<Process id="420" name="Avg.exe" ParentPID="110" />

i want to create a tree view in c# which will look like:

0
|-4
|  |-100
|     |-155
|     |-170
|       |-180
|       |   |-110
|        |      |-420
|        |-200
|            |-380
|-10     
|  |-120
|     |-150  
|-155
|-1764
    |-530  

Any help will greatly be appreciated. Thanks

2
  • Where is the element that has the id = 10 Commented Aug 23, 2012 at 4:24
  • Do you have <Process> tag for id = 10 and id = 1764? Commented Aug 23, 2012 at 4:31

2 Answers 2

2

Here's my solution for you:

    private TreeNode[] FindTreeNodes(string Key)
    {
        return oTreeView.Nodes.Find(Key, true);
    }

    private void LoadTreeview(string XmlFile)
    {
        oTreeView.Nodes.Clear();

        if (File.Exists(XmlFile) == true)
        {
            XmlDocument oXmlDocument = new XmlDocument();
            oXmlDocument.Load(XmlFile);

            XmlNodeList oXmlNodeList = oXmlDocument.SelectNodes("/Processes/Process");

            foreach (XmlNode oXmlNode in oXmlNodeList)
            {
                int iID = Convert.ToInt32(oXmlNode.Attributes["id"].Value);
                string sName = oXmlNode.Attributes["name"].Value;
                int iParentID = Convert.ToInt32(oXmlNode.Attributes["ParentPID"].Value);

                TreeNode[] oParentNodes = FindTreeNodes(iParentID.ToString());
                if (oParentNodes.Length == 0)
                {
                    TreeNode oTreeNode = new TreeNode();

                    oTreeNode.Name = iID.ToString();
                    oTreeNode.Text = String.Format("{0} ({1})", sName, iID);

                    oTreeView.Nodes.Add(oTreeNode);
                }
                else
                {
                    if (oParentNodes.Length > 0)
                    {
                        foreach (TreeNode oParentTreeNode in oParentNodes)
                        {
                            TreeNode oTreeNode = new TreeNode();

                            oTreeNode.Name = iID.ToString();
                            oTreeNode.Text = String.Format("{0} ({1})", sName, iID);

                            oParentTreeNode.Nodes.Add(oTreeNode);
                        }
                    }
                    else
                    {
                        Console.WriteLine("  ** Could not find the parent node {0} for child {1} ({2})", iParentID, sName, iID);
                    }
                }
            }
        }
    }

    protected override void OnLoad(EventArgs E)
    {
        base.OnLoad(E);

        LoadTreeview("Xml.xml");
    }

I've modified the XML a little to look like this - essentially, it's the same:

<?xml version="1.0" encoding="utf-8" ?>

<Processes>
  <Process id="0" name="[System Process]" ParentPID="0" />
  <Process id="4" name="System" ParentPID="0" />
  <!--
  <Process id="10" name="MissingNode" ParentPID="0" />
  <Process id="1764" name="MissingNode" ParentPID="0" />
  -->
  <Process id="100" name="MyApp.exe" ParentPID="4" />
  <Process id="120" name="avgrsx.exe" ParentPID="10"/>
  <Process id="150" name="avgcsrvx.exe" ParentPID="120" />
  <Process id="155" name="csrss.exe" ParentPID="100" />
  <Process id="170" name="winlogon.exe" ParentPID="100" />
  <Process id="180" name="services.exe" ParentPID="170" />
  <Process id="200" name="lsass.exe" ParentPID="170" />
  <Process id="110" name="svchost.exe" ParentPID="180" />
  <Process id="380" name="svchost.exe" ParentPID="200"/>
  <Process id="530" name="svchost.exe" ParentPID="1764" />
  <Process id="420" name="Avg.exe" ParentPID="110" />
</Processes>

Finally, the following nodes do not appear as there are no definitions for them within the XML:

  • 10
  • 1764

These appear commented out within the XML file included above.

Hope this is what you are looking for.

Cheers!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for your valuable reply....i will try this way and soon get back to you
0

Try the following algorithm:

       TreeNode[] arr = new TreeNode[elemCnt]

       arr[0] = new TreeNode with base (root) element

        For each XML element "Process":

                   Let v = Find parent node with linear search in ar


                   If v is not null ( parent found)

                               Add element as child to v

                   Add element to arr

        Add nodes that have null parents to TreeView.Nodes

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.