private static void CreateTree(TreeView treeView, String xmlfile)
{
xmlDoc = new XmlDocument();
xmlDoc.Load(xmlfile);
try
{
numofChild = 0;
treeView.Nodes.Clear();
XmlElement rootXml = xmlDoc.DocumentElement;
TreeNode root = new TreeNode();
root.Text = rootXml.Name;
treeView.Nodes.Add(root);
if (rootXml.ChildNodes.Count > 0)
{
foreach (XmlNode subXmlnode in rootXml.ChildNodes)
{
TreeNode treenode = new TreeNode();
treenode.Text = "<" + subXmlnode.Name + ">";
root.Nodes.Add(treenode);
AddNodeToTreeView(subXmlnode.ChildNodes, treenode);
}
}
else
{
root.Text = rootXml.Value;
}
}
catch (Exception ex)
{
throw ex;
}
}
private static void AddNodeToTreeView(XmlNodeList xmlNodeList, TreeNode parentNode)
{
foreach (XmlNode xmlnode in xmlNodeList)
{
TreeNode subtreenode = new TreeNode();
subtreenode.Text = "<" + xmlnode.Name + ">";
parentNode.Nodes.Add(subtreenode);
if (xmlnode.ChildNodes.Count > 0)
{
AddNodeToTreeView(xmlnode.ChildNodes, subtreenode);
}
else
{
subtreenode.Text = xmlnode.Value;
}
}
}
登录后复制