Click here to Skip to main content
15,883,957 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello friends,

I have deserialized an XML and stored the data in an object (XmlData)
C#
public void CreateDeserializedXmlObject(string strSrcFilename)
{
    try
    {
        //Using this object we can access the different classes & objects created from that XML

        XmlSerializer deserializer = new XmlSerializer(typeof(ABC));
        TextReader reader = new StreamReader(strSrcFilename);
        object obj = deserializer.Deserialize(reader);
        XmlData = (ABC)obj; // XmlData will have all the parsed data in form of Objects
        reader.Close();
    }
    catch (Exception ex)
    {

    }
}


Now, my goal here is to display a treeview from the XmlData object and also the treeview nodes should be binded with the XmlData object.

Currently, I am able to display the treeview from the XML file itself:
C#
        /// <summary>
        /// Creates the TreeView from atxml.
        /// </summary>
        /// <param name="xmlDoc">The XML document.</param>
        private void CreateTreeViewFromATXML(XmlDocument xmlDoc)
        {
            try
            {
                XmlTreeViewBuilder tBuilder = new XmlTreeViewBuilder(xmlDoc, XMLTreeView);
                tBuilder.getTreeView();
            }
            catch (Exception ex)
            { }
        }

class XmlTreeViewBuilder
    {
        private XmlDocument xDoc;
        private TreeView tView;


        //Constructor with parameters
        public XmlTreeViewBuilder(XmlDocument xDocument, TreeView treeView)
        {
            this.xDoc = xDocument;
            this.tView = treeView;
        }

        /// <summary>
        /// Gets the TreeView.
        /// </summary>
        public void getTreeView()
        {
            
            tView.Nodes.Clear();                                        //Clear out the nodes before building
            XmlNode pNode = xDoc.DocumentElement;                       //Set the xml parent node = xml document element
            string Key = pNode.Name == null ? "" : pNode.Name;          //If null set to empty string, else set to name
            string Value = pNode.Value == null ? Key : pNode.Value;     //If null set to node name, else set to value
            TreeNode tNode = tView.Nodes.Add(Key, Value);               //Add the node to the Treeview, set tNode to that node
            AddTreeNodes(pNode, tNode);                                 //Call the recursive function to build the tree
        }



        //Build out the tree recursively
        /// <summary>
        /// Adds the tree nodes.
        /// </summary>
        /// <param name="currentParentNode">The current parent node.</param>
        /// <param name="currentTreeNode">The current tree node.</param>
        private void AddTreeNodes(XmlNode currentParentNode, TreeNode currentTreeNode)
        {
            //Recursively add children, grandchildren, etc...
            if (currentParentNode.HasChildNodes)
            {
                foreach (XmlNode childNode in currentParentNode.ChildNodes)
                {
                    string Key = childNode.Name == null ? "" : childNode.Name;
                    string Value = childNode.Value == null ? Key : childNode.Value;
                    TreeNode treeNode = currentTreeNode.Nodes.Add(Key, Value);
                    //Recursive call to repeat the process for all child nodes which may be parents
                    AddTreeNodes(childNode, treeNode);
                }
            }
        }

    }


Can anyone please provide some suggestions or methods to achieve this.
Thanks.
Posted
Comments
Milfje 5-Jun-15 5:34am    
I don't think there's a way to bind your objects directly to the XML without having to serialize in between that.
[no name] 5-Jun-15 5:41am    
Thanks for ur response. Actually we are asking whether we can bind the deserialized object to the tree view. We have deserialized a xml into a object and that we want to map it to a treeview. As its a very big xml, so the deserialized object is also a big one and its very difficult on our part to map each individual node to the treeview by hardcoding them. We are looking for any smart way/dotnet API, for dealing with this situation.
Milfje 5-Jun-15 5:55am    
Isn't it possible to put the objects in a collection (any IEnumerable implementation, like List) and set the datagrid's DataSource to it? One might also look into a BindingSource, possibly a custom one.
[no name] 5-Jun-15 6:01am    
Good suggestion, but unfortunately our requirement is to show the object in a treeview only. :(
Milfje 5-Jun-15 6:04am    
Perhaps this can help: Data Binding TreeView in C#

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900