1.TreeView的遍历,应该比较简单的吧?我看见不少人写得多长,比较复杂的。现在,举例一个TreeView遍历后勾选:
private void tv1_AfterCheck(object sender, TreeViewEventArgs e)
{
TreeNode curNode = tv1.SelectedNode = e.Node;
bool isCheched = curNode.Checked;
this.tv_outScr.AfterCheck -= new System.Windows.Forms.TreeViewEventHandler(this.tv_outScr_AfterCheck); //避免递归勾选问题
SubNodes_Check(curNode, isCheched); //遍历递归勾选当前选项下的所有子节点
}
private void SubNodes_Check(TreeNode tn, bool wantCheck) //递归勾选
{
foreach (TreeNode tnSub in tn.Nodes)
{
tnSub.Checked = wantCheck;
SubNodes_Check(tnSub, wantCheck);
}
}
2.Xml文件内容在treeView树上显示(Xml2TreeView):
//添加1按钮
private void btn_open_Click(object sender, EventArgs e)
{
tv1.Nodes.Clear();
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "文本文档 (*.xml)|*.xml";
if (ofd.ShowDialog() == DialogResult.OK)
{
tb_prgFileName.Text = ofd.FileName;
XDocument xmldoc = XDocument.Load(tb_prgFileName.Text); //加载xml文件
XElement rootElement = xmldoc.Root;
TreeNode rootNode = new TreeNode(rootElement.Name.ToString());
Xml2TreeView(rootElement, rootNode.Nodes); //递归
rootNode.Text += " -- " + rootNode.Nodes.Count.ToString();
tv1.Nodes.Add(rootNode); //一定要最后一步才添加节点,否则,节点内容上w非常慢
// tv1.ExpandAll();//展开TreeView控件中的所有项
}
}
private void Xml2TreeView(XElement rootElement, TreeNodeCollection treeNodeCollection)
{
//获取根元素rootElement 下的所有子元素
//rootElement.Elements()
//遍历rootElement下的所有子元素(直接元素)
TreeNode node_L1 = new TreeNode();
TreeNode node_L2 = new TreeNode();
foreach (XElement item in rootElement.Elements())
{
if (item.Elements().Count() == 0)
{
//将当前元素加载到TreeView的节点集合中
node_L1 = treeNodeCollection.Add(item.Name.ToString());
node_L1.Name = item.Name.ToString(); //节点名称
// if (!string.IsNullOrEmpty(item.Value)) //只添加非空值
{
node_L1.Nodes.Add(item.Value);
node_L1.Tag = item.Value;
}
if (item.FirstAttribute != null)
{
XAttribute att = item.FirstAttribute;
do
{
node_L2 = new TreeNode(att.Name.ToString());
node_L2.Name = att.Name.ToString();
node_L2.Nodes.Add(att.Value);
node_L2.Tag = att.Value;
node_L1.Nodes.Add(node_L2);
}
while ((att = att.NextAttribute) != null);
}
Xml2TreeView(item, node_L1.Nodes);
}
else
{
//将当前元素加载到TreeView的节点集合中
node_L1 = treeNodeCollection.Add(item.Name.ToString());
node_L1.Name = item.Name.ToString(); //节点名称
if (item.FirstAttribute != null)
{
XAttribute att = item.FirstAttribute;
do
{
node_L2 = new TreeNode(att.Name.ToString());
node_L2.Name = att.Name.ToString();
node_L2.Nodes.Add(att.Value);
node_L2.Tag = att.Value;
node_L1.Nodes.Add(node_L2);
}
while ((att = att.NextAttribute) != null);
}
Xml2TreeView(item, node_L1.Nodes);
}
}
}
//示例的xml文件内容,其他的xml文件也是可以调用的
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tv SYSTEM "http://api.torrent-tv.ru/xmltv.dtd">
<tv generator-info-name="zzy" generator-info-url="QQ 1728570648">
<channel id="1"><display-name lang="zh">CCTV1</display-name></channel>
<channel id="2"><display-name lang="zh">CCTV2</display-name></channel>
<channel id="3"><display-name lang="zh">CCTV3</display-name></channel>
<programme start="20190926000000 +0800" stop="20190926002500 +0800" channel="1">
<title lang="zh">凤凰子夜快车(26/9/19)</title>
<desc lang="zh"> 营业员 </desc>
</programme>
<programme start="20190926002500 +0800" stop="20190926003000 +0800" channel="2">
<title lang="zh">丝路文明(1062)</title>
<desc lang="zh"> </desc>
</programme>
<programme start="20190926003000 +0800" stop="20190926005500 +0800" channel="3">
<title lang="zh">有报天天读(25/9/19)</title>
<desc lang="zh"> </desc>
</programme>
<programme start="20190926005500 +0800" stop="20190926015000 +0800" channel="4">
<title lang="zh">全媒体大开讲(短版)(1210)</title>
<desc lang="zh"> </desc>
</programme>
<programme start="20190926031000 +0800" stop="20190926040000 +0800" channel="5">
<title lang="zh">实况录像-2019年世界攀岩锦标赛 男女速度决赛</title>
<desc lang="zh"> </desc>
</programme>
<programme start="20190926010000 +0800" stop="20190926031000 +0800" channel="6">
<title lang="zh">2019/2020法国足球甲级联赛-第7轮(里尔—斯特拉斯堡)</title>
<desc lang="zh"> </desc>
</programme>
<programme start="20190926053000 +0800" stop="20190926070000 +0800" channel="6">
<title lang="zh">国际足球赛场-19-20赛季意甲联赛第4轮 尤文图斯—维罗纳</title>
<desc lang="zh"> </desc>
</programme>
<programme start="20190926070000 +0800" stop="20190926083000 +0800" channel="6">
<title lang="zh">录像-2019年乒乓球亚锦赛 男子团体决赛</title>
<desc lang="zh"> </desc>
</programme>
</tv>
3.处理treeview上的内容,若channel id值相同,则判断为一个频道下的内容,添加其节目内容。这里用递归处理。
private void btn_process_Click(object sender, EventArgs e)
{
if (tv1.Nodes == null) return;
tv_XmlEpg.Nodes.Clear();
tv_XmlEpg.Nodes.Add("MyTV");
DiGui_TreeView(tv_XmlIn.Nodes[0], tv_XmlEpg.Nodes[0].Nodes); //递归处理
tv_XmlEpg.Nodes[0].Text += " -- " + tv_XmlEpg.Nodes[0].Nodes.Count.ToString();
}
private void DiGui_TreeView(TreeNode tn, TreeNodeCollection tnCol)
{
TreeNode tn_ch = new TreeNode();
TreeNode tn_event = new TreeNode();
foreach (TreeNode tnSub in tn.Nodes)
{
switch (tnSub.Name)
{
//1.频道信息
case "channel":
if (tnSub.Level == 1)
{
tn_ch = new TreeNode();
}
else if (tnSub.Level == 2)
{
foreach (TreeNode tn_ch_L1 in tnCol)
{
if (tnSub.FirstNode.Text == (string)tn_ch_L1.Tag)
{
tn_event.Text = "event -- " + (tn_ch_L1.Nodes.Count + 1).ToString();
tn_ch_L1.Nodes.Add(tn_event);
break;
}
}
}
break;
case "id":
tn_ch.Text = "id = " + tnSub.FirstNode.Text;
tn_ch.Name = "Channel";
tn_ch.Tag = tnSub.FirstNode.Text;
tnCol.Add(tn_ch);
break;
case "display-name":
tn_ch.Text += " -- [" + tnSub.FirstNode.Text + "]";
break;
//2.事件信息
case "programme":
tn_event = new TreeNode();
//tnCol.Add(tn_event);
break;
case "start":
tn_event.Nodes.Add("start = " + tnSub.FirstNode.Text);
break;
case "stop":
tn_event.Nodes.Add("stop = " + tnSub.FirstNode.Text);
break;
case "title":
tn_event.Nodes.Add("title = " + tnSub.FirstNode.Text);
break;
case "desc":
tn_event.Nodes.Add("desc = " + tnSub.FirstNode.Text);
break;
}
DiGui_TreeView(tnSub, tnCol);
}
}