public void BuildTreeView()
{
try
{
string _XPath = "xml";
string _rootNode = "FAQList";
string _filePath = Server.MapPath("XMLFiles/FAQ_List.xml"); //@"e:\\x.xml";
// Load the XML file .
XmlDocument doc = new XmlDocument();
doc.Load(_filePath);
// Load the XML into the TreeView.
faqList.Nodes.Clear();
faqList.Nodes.Add(new TreeNode(_rootNode));
TreeNode node = new TreeNode();
node = faqList.Nodes[0];
XmlNodeList nodes = doc.SelectNodes(_XPath);
XmlNode xnode = nodes.Item(0).ParentNode;
AddNode(ref xnode, ref node);
faqList.CollapseAll();
faqList.Nodes[0].Expand();
}
catch (Exception ex)
{
lblMessage.Text = ex.Message;
}
}
[code]
Now the code that adds the nodes (aptly named [b]AddNode[/b]) follows:
[code]
private void AddNode(ref XmlNode inXmlNode, ref TreeNode inTreeNode)
{
// 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;
for (i = 0; i <= nodeList.Count - 1; i++)
{
xNode = inXmlNode.ChildNodes[i];
faqList.Nodes.Add(new TreeNode(xNode.Name));
tNode = faqList.Nodes[i];
AddNode(ref xNode, ref tNode);
}
}
else
{
inTreeNode.Text = inXmlNode.OuterXml.Trim();
}
}
Anyone see what I'm doing here?

New Topic/Question
Reply


MultiQuote








|