Im using c#.net windows form application. I have a xml file. I have loaded unique node names into a tree view. Here is the xml file: Hello.xml - - abc hello how ru - def i m fine - ghi how abt u - asd aaaaaaaaa
I have loaded the node names into a tree view. The code I have used is:
string xmlfilename = ""; private void button1_Click(object sender, EventArgs e) {
try
{
string strRootNode = "resourceRoot";
OpenFileDialog Dlg = new OpenFileDialog();
Dlg.Filter = "All files(*.*)|*.*|xml file (*.xml)|*.txt";
Dlg.CheckFileExists = true;
if (Dlg.ShowDialog() == DialogResult.OK)
{
xmlfilename = Dlg.FileName;
}
// Load the XML file.
//XmlDocument dom = new XmlDocument();
//dom.Load(xmlfilename);
XmlDocument doc = new XmlDocument();
doc.Load(xmlfilename);
//this will give the root name
string rootName = doc.SelectSingleNode("/*").Name;
textBox4.Text = rootName.ToString();
//XmlNode root = dom.LastChild;
//textBox4.Text = root.Name.ToString();
//List<String> listOfNodes = new List<string>();
// Load the XML into the TreeView.
this.treeView1.Nodes.Clear();
this.treeView1.Nodes.Add(new TreeNode(strRootNode));
TreeNode mytreeNode = new TreeNode();
mytreeNode = this.treeView1.Nodes[0];
XmlNodeList oNodes = doc.SelectNodes(textBox4.Text);
XmlNode myxmlNode = oNodes.Item(0);
TreeNode tNewNode = new TreeNode(myxmlNode.Name);
mytreeNode.Nodes.Add(tNewNode);
AddNode(ref myxmlNode, ref tNewNode);
//this.treeView1.CollapseAll();
//this.treeView1.Nodes[0].Expand();
//this.Cursor = System.Windows.Forms.Cursors.Default;
}
catch (Exception ex)
{
this.Cursor = System.Windows.Forms.Cursors.Default;
}
}
private void AddNode(ref XmlNode inXmlNode, ref TreeNode inTreeNode)
{
try
{
// Recursive routine to walk the XML DOM and add its nodes to a TreeView.
XmlNode xNode;
TreeNode tNode;
XmlNodeList nodeList;
int i;
// Loop through the XML nodes until the leaf is reached.
// Add the nodes to the TreeView during the looping process.
if (inXmlNode.HasChildNodes)
{
nodeList = inXmlNode.ChildNodes;
List<string> nodeNames = new List<string>();
for (i = 0; i < nodeList.Count; i++)
{
xNode = inXmlNode.ChildNodes[i];
//if (xNode.NodeType != (XmlNodeType.Text))
//{
//if (inTreeNode.Nodes.Contains( xNode.Name))
//{
//listOfNodes.Add(xNode.Name.ToString());
if (xNode.HasChildNodes)
{
if (!(nodeNames.Contains(xNode.Name.ToString())))
{
inTreeNode.Nodes.Add(new TreeNode(xNode.Name));
tNode = inTreeNode.Nodes[inTreeNode.Nodes.Count - 1];
//if ((listOfNodes.Contains(tNode.Name.ToString())))
//{
AddNode(ref xNode, ref tNode);
//}
nodeNames.Add(xNode.Name.ToString());
}
}
}
}
else
{
//MessageBox.Show("not working");
inTreeNode.Text = inXmlNode.OuterXml.Trim();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
}
AND THE OUTPUT OF THE TREE VIEW IS: -resourceRoot -languages language key value name firstname middlename
Now my problem is if I select key, I need to populate the combobox with these values: abc def ghi
I mean to say all the values in the key node. Please suggest me with a proper code.