<?xml version="1.0" encoding="utf-8" ?>
<movie>
<gongfu name="功夫">
<time>8-00 9-45</time>
<time>10-00 11-45</time>
</gongfu>
<fkdst name="疯狂的石头">
<time>8-00 9-45</time>
<time>10-00 11-45</time>
</fkdst>
<zzx name="蜘蛛侠3">
<time>8-00 9-45</time>
<time>10-00 11-45</time>
</zzx>
</movie>这是XML文件,怎么绑定在treeview里啊,xiexie
<movie>
<gongfu name="功夫">
<time>8-00 9-45</time>
<time>10-00 11-45</time>
</gongfu>
<fkdst name="疯狂的石头">
<time>8-00 9-45</time>
<time>10-00 11-45</time>
</fkdst>
<zzx name="蜘蛛侠3">
<time>8-00 9-45</time>
<time>10-00 11-45</time>
</zzx>
</movie>这是XML文件,怎么绑定在treeview里啊,xiexie
/// 从XML中读取数据到TreeView
/// </summary>
/// <param name="openXmlDlg">打开文件对话框</param>
/// <param name="treeViewControl">某一层次的节点集合</param>
public void ReadXmlToTreeView(OpenFileDialog openXmlDlg, TreeView treeViewControl)
{
openXmlDlg.DefaultExt = "xml";
openXmlDlg.Filter = "XML Files (*.xml)|*.xml";
openXmlDlg.FilterIndex = 1;
if (openXmlDlg.ShowDialog() == DialogResult.OK)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(openXmlDlg.FileName); // 装载XML文件
treeViewControl.BeginUpdate();
treeViewControl.Nodes.Clear();
XmlNode node = xmlDoc.DocumentElement; // 读取XML文件的根结点
// string tempText = (node.Value != null ? node.Value : (node.Attributes != null && node.Attributes.Count > 0) ? node.Attributes[5].Value : node.Name);
string tempText = node.Name;
TreeNode rootNode = new TreeNode(tempText);
rootNode.Name = node.Name;
treeViewControl.Nodes.Add(rootNode); // 首先把根结点填入TreeView控件
rootNode.ToolTipText = tempText;
XmlNodeToTreeNode(xmlDoc.DocumentElement, rootNode.Nodes); // 利用递归的方法读取XML所有节点并填入TreeView控件
treeViewControl.EndUpdate();
}
} /// <summary>
/// 递归的方法读取XML所有节点并填入TreeView控件
/// </summary>
/// <param name="documentElement">xml文档中的单个节点</param>
/// <param name="nodes">所有节点集合</param>
public void XmlNodeToTreeNode(XmlNode documentElement, TreeNodeCollection nodes)
{
foreach (XmlNode node in documentElement.ChildNodes)
{
//string tempText = (node.Value != null ? node.Value : (node.Attributes != null && node.Attributes.Count > 0) ? node.Attributes[5].Value : node.Name);
string tempText = node.Name;
//创建一个树节点,参数tempText代表树节点的Text属性
TreeNode newChild = new TreeNode(tempText);
//为树节点的Name属性赋值
newChild.Name = node.Name;
newChild.ToolTipText = tempText;
//添加树节点
nodes.Add(newChild);
XmlNodeToTreeNode(node, newChild.Nodes);
} }