Hi,
i have a project in which i created two classes TreeDisplayform(Form.cs) and MytreeNode class in same namespace. TreeDisplay class contains all the GUI related stuff like Browse button textbox, TReeView. I want the user to select a XML file through browse button which will be displayed in textbox.The Xml file selected will be displayed in Treeview on winform. Now i want to separate the GUI part from business logic(where i am creating treenode and adding nodes to tree) thats why i created the MytreeNode class. I am passing string filename from the FileOpenDialogue in Browse button.

//Browse button click event
 if (open.ShowDialog(this) == DialogResult.OK)
        {   txtFileName.Text = open.FileName;
            new MytreeNodeClass(open.FileName);//this variable gives the name of selected xml file to MytreeNodeclass
        }

and MytreeNodeclass code is here

public class MytreeNodeClass
{
    readonly TreeDisplayForm formObj = new TreeDisplayForm();//created object of Treedisplayform class

    public MytreeNodeClass(string filepath)
    {
        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.Load(filepath);
        formObj.passingTreeview(xmlDocument);

       XmlNode xNode = xmlDocument.DocumentElement;
     AddNodes(xNode, formObj.tNodeObj);//calling treenode of Treedisplayform class            
    }

and the passingTreeview() function is here in Treedisplayform class

public void passingTreeview(XmlDocument xmlDocument)
    {
       treeView1.Nodes.Clear();

       if (xmlDocument.DocumentElement != null)
       if (true) treeView1.Nodes.Add(new TreeNode(xmlDocument.DocumentElement.Name));

        tNodeObj = new TreeNode();//Declared public TreeNode tNodeObj; on top in TreeDisplayform class.
        tNodeObj = treeView1.Nodes[0];
        XmlNode xNode = xmlDocument.DocumentElement;

        treeView1.Nodes[0].Expand();
        treeView1.CollapseAll();
    }//passingTreeview

but i am unable to see the treeview in the winform.The code runs successfully in TreeDisplayform.cs but on separating there is no error but result is not shown.
Can you tell me where i am getting wrong??
Thanks....

Dani AI

Generated

The problem is not the XML load itself but the way the UI instance is being updated. In the code shown by the worker class creates or manipulates a form instance that is not the same as the form that was shown by the Browse button. That means the visible TreeView never receives the nodes. Also double-setting a temporary TreeNode and calling a global CollapseAll after expanding can hide the node you just added.

A cleaner solution is to keep parsing logic out of the UI class entirely: have a non-UI class build and return a TreeNode (or an IEnumerable<TreeNode>) and let the form add that node to its TreeView. This keeps responsibilities separate and avoids accidental creation of a new Form instance.

Example builder (non-UI) pattern:

public class XmlTreeBuilder
{
    public TreeNode Build(XmlDocument doc)
    {
        if (doc?.DocumentElement == null) return null;
        var root = new TreeNode(doc.DocumentElement.Name);
        AddChildren(doc.DocumentElement, root);
        return root;
    }

    void AddChildren(XmlNode xml, TreeNode tree)
    {
        foreach (XmlNode child in xml.ChildNodes)
        {
            var n = new TreeNode(child.Name);
            tree.Nodes.Add(n);
            AddChildren(child, n);
        }
    }
}

Usage from the form: load the XmlDocument, call the builder, then add the returned root to the form's TreeView (use Invoke if loading on a background thread). Verify you are updating the same TreeView instance that is visible, avoid re-creating the form, and do not call CollapseAll right after Expand if you want the node visible. If further issues persist, check that DocumentElement is not null and step through to confirm the form instance and TreeView reference are the ones shown on screen. offered to review a sample project — sharing a minimal repro will make it easy to point out the exact instance mismatch.

If you post a full project sample here I will be willing to take a look at it. With delayed initialization and how .NET creates controls this could be one of a number of things.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.