lim

lim

  • NA
  • 16
  • 0

Tree View using xml file

Dec 27 2005 4:54 AM
Hi all,

I am using an xml file to actually load the data to a tree view.
My xml file content is as follow:

<?xml version="1.0"?>
<Parent>
<ABCD10>
   <PC1>
     BASIC 
   </PC1>
   <PC3>
     RF1
   </PC3>
  </ABCD10>       
  </Parent>

Under ABCD10, PC1, I would like to add in BASIC, BASIC1 and BASIC2 as the child for ABCD10, PC1.
But when I add in BASIC1 under BASIC, and run the program, the treeview just shown "BASIC BASIC1" instead of create a branches with BASIC and BASIC1. Can anyone help whether it is xml format problem or the program coding ?
The program coding is as follow:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Xml;

namespace EPC_Control_Station

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

try

{

// SECTION 1. Create a DOM Document and load the XML data into it.

XmlDocument dom = new XmlDocument();

dom.Load("D:\\EPC\\abc.xml");

// SECTION 2. Initialize the TreeView control.

treeView1.Nodes.Clear();

treeView1.Nodes.Add(new TreeNode(dom.DocumentElement.Name));

TreeNode tNode = new TreeNode();

tNode = treeView1.Nodes[0];

// SECTION 3. Populate the TreeView with the DOM nodes.

AddNode(dom.DocumentElement, tNode);

treeView1.ExpandAll();

}

catch (XmlException xmlEx)

{

MessageBox.Show(xmlEx.Message);

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}

private void button1_Click(object sender, EventArgs e)

{

}

private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)

{

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;

for(i = 0; i<=nodeList.Count - 1; i++)

{

xNode = inXmlNode.ChildNodes[i];

inTreeNode.Nodes.Add(new TreeNode(xNode.Name));

tNode = inTreeNode.Nodes[i];

AddNode(xNode, tNode);

}

}

else

{

// Here you need to pull the data from the XmlNode based on the

// type of node, whether attribute values are required, and so forth.

inTreeNode.Text = (inXmlNode.OuterXml).Trim();

}

}

}

}