Hello friends,
I have deserialized an XML and stored the data in an object (XmlData)
public void CreateDeserializedXmlObject(string strSrcFilename)
{
try
{
XmlSerializer deserializer = new XmlSerializer(typeof(ABC));
TextReader reader = new StreamReader(strSrcFilename);
object obj = deserializer.Deserialize(reader);
XmlData = (ABC)obj;
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:
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;
public XmlTreeViewBuilder(XmlDocument xDocument, TreeView treeView)
{
this.xDoc = xDocument;
this.tView = treeView;
}
public void getTreeView()
{
tView.Nodes.Clear();
XmlNode pNode = xDoc.DocumentElement;
string Key = pNode.Name == null ? "" : pNode.Name;
string Value = pNode.Value == null ? Key : pNode.Value;
TreeNode tNode = tView.Nodes.Add(Key, Value);
AddTreeNodes(pNode, tNode);
}
private void AddTreeNodes(XmlNode currentParentNode, TreeNode currentTreeNode)
{
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);
AddTreeNodes(childNode, treeNode);
}
}
}
}
Can anyone please provide some suggestions or methods to achieve this.
Thanks.