Hello,
I have a TreeView Control with checkbox in my winform application .TreeView Nodes are Populated from Sql DataBase.I Have one xml file which contains information about checked nodes of treeview.Now i want to check all treenode based on xml node's value.Please give me any sample code.Thanks in advance.
Loading

Posted Sep 25, 2013, 4:24 AM
Remove the below one.
treeNode.Text = xmlNode.OuterXml.Trim();
else //No children, so add the outer xml (trimming off whitespace)
treeNode.Text = xmlNode.Name;
Hope this helps you !
Surendrasinh RathodPosted Sep 25, 2013, 4:32 AM
Its very important for me.Since last 2 hours i was checking whole code but not do attention on else part.Anyway very very thanks to you for give the answer on the sport.
Posted Sep 25, 2013, 4:17 AM
Surendrasinh RathodPosted Sep 25, 2013, 4:17 AM
Surendrasinh RathodPosted Sep 25, 2013, 4:05 AM
All XML Nodes are successfully Add in TreeView but every node's child node are displaying with XML Attributes.I have attach a snapshot of treeview .What i have done mistake.can u reply please.Thanks in advance.
My xml file is like following
-
-
-
-
-
-
-
And my c# code to dispaly this xml file to TreeView as per following
private void DefaultNodesFromXML()
{
//string path = "E:\\Surendra\\WareHouseManagement\\WareHouse.Admin.UI\\" + cmbEmpName.Text + "_" + cmbEmpName.SelectedValue + ".xml";
string path = "E:\\Surendra\\WareHouseManagement\\WareHouse.Admin.UI\\Surendra_2.xml";
XmlDocument xDoc = new XmlDocument();
xDoc.Load(path);
tvMenu.Nodes.Clear();
//XmlNode root = xDoc.FirstChild;
//if (root.HasChildNodes)
//{
tvMenu.Nodes.Add(new TreeNode(xDoc.DocumentElement.Name));
TreeNode tNode = new TreeNode();
tNode = (TreeNode)tvMenu.Nodes[0];
AddTreeNode(xDoc.DocumentElement, tNode);
}
private void AddTreeNode(XmlNode xmlNode, TreeNode treeNode)
{
XmlNode xNode;
TreeNode tNode;
XmlNodeList xNodeList;
if (xmlNode.HasChildNodes) //The current node has children
{
xNodeList = xmlNode.ChildNodes;
for (int x = 0; x <= xNodeList.Count - 1; x++)
//Loop through the child nodes
{
xNode = xmlNode.ChildNodes[x];
treeNode.Nodes.Add(new TreeNode(xNode.Name));
tNode = treeNode.Nodes[x];
if (xNode.Attributes["ID"].Value == "True")
{
tNode.Checked = true;
}
//xNode.Attributes.RemoveAll();
AddTreeNode(xNode, tNode);
}
}
else //No children, so add the outer xml (trimming off whitespace)
treeNode.Text = xmlNode.OuterXml.Trim();
}
All XML Nodes are successfully Add in TreeView but every node's child node are displaying with XML Attributes.I have attach a snapshot of treeview .What i have done mistake.can u reply please.Thanks in advance.
Surendrasinh RathodPosted Sep 25, 2013, 3:36 AM
Posted Sep 25, 2013, 2:35 AM
void Button1_Click(object sender, EventArgs e)
{
TreeNode rootNode = treeView1.Nodes.Add("Root");
TreeNode colourNode = rootNode.Nodes.Add("Colour");
colourNode.Nodes.Add("Red");
colourNode.Nodes.Add("Blue");
colourNode.Nodes.Add("Green");
TreeNode countryNode = rootNode.Nodes.Add("Country");
countryNode.Nodes.Add("India");
countryNode.Nodes.Add("UK");
countryNode.Nodes.Add("USA");
TreeNode fruitsNode = rootNode.Nodes.Add("Fruits");
fruitsNode.Nodes.Add("Apple");
fruitsNode.Nodes.Add("Mango");
fruitsNode.Nodes.Add("Banana");
treeView1.ExpandAll();
}
//Read the value from the xml file and find out in the tree, update the checkboxes.
void Button2_Click(object sender, System.EventArgs e)
{
//Read the xml value here.
string xmlValue = "Country";
TreeNode root = treeView1.Nodes[0];
if (root.Nodes.Count > 0)
{
foreach (TreeNode element in root.Nodes) {
if (element.Text == xmlValue)
{
treeView1.SelectedNode = element;
treeView1.SelectedNode.Checked = true;
if (element.Nodes.Count >0)
{
foreach (TreeNode child in element.Nodes) {
treeView1.SelectedNode = child;
treeView1.SelectedNode.Checked = true;
}
}
break;
}
}
}
}
Hope this helps you.
Surendrasinh RathodPosted Sep 25, 2013, 1:50 AM
Posted Sep 25, 2013, 1:40 AM
Do you want check the parent and check boxes based on the value which is present in the xml file?