1 <?xml version="1.0" encoding="utf-8" ?>Tv
2 <TVChannels>
3 <Channel>
4 <channelType>TypeA</channelType>
5 <tvChannel>北京电视台</tvChannel>
6 <path>TypeA.xml</path>
7 </Channel>
8
9 <Channel>
10 <channelType>TypeB</channelType>
11 <tvChannel>凤凰电视台</tvChannel>
12 <path>TypeB.xml</path>
13 </Channel>
14 </TVChannels>
1 <?xml version="1.0" encoding="utf-8" ?>TypeA
2 <!--频道A-->
3 <typeA>
4 <!--电视台名称-->
5 <channelName>北京电视台</channelName>
6 <!--电视台节目清单-->
7 <tvProgramTable>
8 <Program>
9 <playTime>2017-04-15 6:02</playTime><!--播出时间-->
10 <ProgramName>《熊出没》</ProgramName><!--节目名称-->
11 <ProgramPath>d:/tv/《熊出没》.mp4</ProgramPath><!--节目路径-->
12 </Program>
13
14 <Program>
15 <playTime>2017-04-16 7:00</playTime><!--播出时间-->
16 <ProgramName>《sugar》</ProgramName><!--节目名称-->
17 <ProgramPath>d:/tv/《sugar》.mp4</ProgramPath><!--节目路径-->
18 </Program>
19
20 <Program>
21 <playTime>2017-04-17 8:08</playTime><!--播出时间-->
22 <ProgramName>《西游记》</ProgramName><!--节目名称-->
23 <ProgramPath>d:/tv/《西游记》.mp4</ProgramPath><!--节目路径-->
24 </Program>
25 </tvProgramTable>
26 </typeA>
1 <?xml version="1.0" encoding="utf-8" ?>TypeB
2 <!--频道B-->
3 <typeB>
4 <!--电视台名称-->
5 <channelName>凤凰电视台</channelName>
6 <!--电视台节目清单-->
7 <tvProgramTable>
8 <program>
9 <playTime>2017-04-16 20:00</playTime><!--播出时间-->
10 <ProgramName>《雪豹》</ProgramName><!--节目名称-->
11 <ProgramPath>d:/tv/《雪豹》.mp4</ProgramPath><!--节目路径-->
12 </program>
13
14 <program>
15 <playTime>2017-04-16 9:00</playTime><!--播出时间-->
16 <ProgramName>《三国》</ProgramName><!--节目名称-->
17 <ProgramPath>d:/tv/《三国》.mp4</ProgramPath><!--节目路径-->
18 </program>
19
20 <program>
21 <playTime>2017-04-17 10:15</playTime><!--播出时间-->
22 <ProgramName>《水浒传》</ProgramName><!--节目名称-->
23 <ProgramPath>d:/tv/《水浒传》.mp4</ProgramPath><!--节目路径-->
24 </program>
25 </tvProgramTable>
26 </typeB>
----------类----------
1 using System;ChannelBase(父类)
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace TellyFairy.Entity
8 {
9 public abstract class ChannelBase
10 {
11 /// <summary>
12 /// 频道名称
13 /// </summary>
14 public string ChannelName { get; set; }
15 /// <summary>
16 /// Xml文件路径
17 /// </summary>
18 public string Path { get; set; }
19 /// <summary>
20 /// 节目列表
21 /// </summary>
22 public List<TvProgram> ProgramList { get; set; }
23
24 public ChannelBase()
25 {
26 this.ProgramList = new List<TvProgram>();
27 }
28 /// <summary>
29 /// 解析频道节目单信息
30 /// </summary>
31 public abstract void Fetch();
32
33 }
34 }
1 using System;ChannelManager类
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using System.Xml;
7
8 namespace TellyFairy.Entity
9 {
10 public class ChannelManager
11 {
12 //XML文件路径
13 public string channelPath = "Tv.xml";
14 //所有频道集合
15 public Dictionary<string, ChannelBase> channelBase = new Dictionary<string, ChannelBase>();
16 /// <summary>
17 /// 根据频道类型创建频道
18 /// </summary>
19 /// <param name="type"></param>
20 /// <returns></returns>
21 public ChannelBase CreateChannel(string type)
22 {
23 ChannelBase clb = null;
24 if (type == "TypeA")
25 {
26 clb = new TypeAChannel();
27 }
28 if (type == "TypeB")
29 {
30 clb = new TypeBChannel();
31 }
32 return clb;
33 }
34 /// <summary>
35 /// 加载节目
36 /// </summary>
37 public void LoadChannel()
38 {
39 XmlDocument xmlDoc = new XmlDocument();
40 xmlDoc.Load(this.channelPath);
41 XmlNode xmlRoot = xmlDoc.DocumentElement;
42 //将所有频道添加到channelBase泛型集合
43 foreach (XmlNode node in xmlRoot.ChildNodes)
44 {
45 //根据类型创建不同子类
46 ChannelBase clb = CreateChannel(node["channelType"].InnerText);
47 //根据创建不同子类的对象将不同频道名称赋值给属性
48 clb.ChannelName = node["tvChannel"].InnerText;
49 //根据创建不同子类的对象把不同频道的xml地址赋值属性
50 clb.Path = node["path"].InnerText;
51 //根据不同子类对象执行重写方法 给 节目列表赋值
52 clb.Fetch();
53 //将不同频道 赋值给所有频道泛型集合
54 channelBase.Add(clb.ChannelName, clb);
55 }
56 }
57
58 }
59 }
1 using System;TvProgram类
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace TellyFairy.Entity
8 {
9 public class TvProgram
10 {
11 /// <summary>
12 /// 播出时间
13 /// </summary>
14 public DateTime PlayTime { get; set; }
15 /// <summary>
16 /// 节目名称
17 /// </summary>
18 public string ProgramName { get; set; }
19 /// <summary>
20 /// 节目路径
21 /// </summary>
22 public string ProgramPath { get; set; }
23 }
24 }
1 using System;TypeAChannel类
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using System.Xml;
7
8 namespace TellyFairy.Entity
9 {
10 public class TypeAChannel:ChannelBase
11 {
12 public override void Fetch()
13 {
14 XmlDocument xmlDoc = new XmlDocument();
15 xmlDoc.Load(base.Path);
16 //XmlElement elem = xmlDoc.DocumentElement;
17 XmlNode xmlRoot = xmlDoc.DocumentElement;
18 ProgramList = new List<TvProgram>();
19 foreach (XmlNode node in xmlRoot.ChildNodes)
20 {
21 if (node.Name == "tvProgramTable")
22 {
23 foreach (XmlNode node1 in node.ChildNodes)
24 {
25 //新建节目类型对象
26 TvProgram program = new TvProgram();
27 //根据xml数据赋值对应属性
28 program.PlayTime = DateTime.Parse(node1["playTime"].InnerText);
29 program.ProgramName = node1["ProgramName"].InnerText;
30 program.ProgramPath = node1["ProgramPath"].InnerText;
31 //将节目添加到节目列表
32 ProgramList.Add(program);
33 }
34 }
35 }
36 }
37 }
38 }
1 using System;TypeBChannel类
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using System.Xml;
7
8 namespace TellyFairy.Entity
9 {
10 public class TypeBChannel:ChannelBase
11 {
12 public override void Fetch()
13 {
14 XmlDocument xmlDoc = new XmlDocument();
15 xmlDoc.Load(base.Path);
16 //XmlElement elem = xmlDoc.DocumentElement;
17 XmlNode xmlRoot = xmlDoc.DocumentElement;
18 ProgramList = new List<TvProgram>();
19 foreach (XmlNode node in xmlRoot.ChildNodes)
20 {
21 if (node.Name == "tvProgramTable")
22 {
23 foreach (XmlNode node1 in node.ChildNodes)
24 {
25 //新建节目类型对象
26 TvProgram program = new TvProgram();
27 //根据xml数据赋值对应属性
28 program.PlayTime = DateTime.Parse(node1["playTime"].InnerText);
29 program.ProgramName = node1["ProgramName"].InnerText;
30 program.ProgramPath = node1["ProgramPath"].InnerText;
31 //将节目添加到节目列表
32 ProgramList.Add(program);
33 }
34 }
35 }
36 }
37 }
38 }
1 using System;Mian
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Threading.Tasks;
9 using System.Windows.Forms;
10 using TellyFairy.Entity;
11
12 namespace TellyFairy
13 {
14 public partial class FrmMain : Form
15 {
16 public FrmMain()
17 {
18 InitializeComponent();
19 //初始把ContextMenuStrip两个选项隐藏
20 cmsiMenu.Items[0].Visible = false;
21 cmsiMenu.Items[1].Visible = false;
22 }
23 ChannelManager clm = new ChannelManager();
24
25 /// <summary>
26 /// 初始化TreeView 选项
27 /// </summary>
28 public void InitList()
29 {
30 tvTv.Nodes.Add("我的电视台");
31 tvTv.Nodes.Add("所有电视台");
32 //加载节目
33 clm.LoadChannel();
34 //将所有频道名称加到所有电视台
35 foreach (var cb in clm.channelBase)
36 {
37 tvTv.Nodes[1].Nodes.Add(cb.Key);
38 }
39 //展开所有节点
40 tvTv.ExpandAll();
41 }
42 /// <summary>
43 /// 窗体加载
44 /// </summary>
45 /// <param name="sender"></param>
46 /// <param name="e"></param>
47 private void FrmMain_Load(object sender, EventArgs e)
48 {
49 dgvProgram.AutoGenerateColumns = false;
50 InitList();
51 }
52 //Items[0]:删除,Items[1]:添加
53 private void tvTv_MouseClick(object sender, MouseEventArgs e)
54 {
55
56 //如果选中的是我的电视台的子节点则把删除显示,加入隐藏
57 if (tvTv.SelectedNode.Parent != null && tvTv.SelectedNode.Parent.Text.Equals("我的电视台"))
58 {
59 cmsiMenu.Items[0].Visible = false;
60 cmsiMenu.Items[1].Visible = true;
61 }
62 //如果选中的是所有电视台的子节点则把隐藏显示,删除隐藏
63 else
64 {
65 cmsiMenu.Items[1].Visible = false;
66 cmsiMenu.Items[0].Visible = true;
67 }
68 //如果选中根节点则把两个都隐藏
69 if (tvTv.SelectedNode.Level == 0)
70 {
71 cmsiMenu.Items[0].Visible = false;
72 cmsiMenu.Items[1].Visible = false;
73 }
74
75
76 }
77 /// <summary>
78 /// 右键添加到电视台
79 /// </summary>
80 /// <param name="sender"></param>
81 /// <param name="e"></param>
82 private void tmsiAdd_Click(object sender, EventArgs e)
83 {
84 //循环判断“我的电视台”里有无重复频道
85 foreach (TreeNode tr in tvTv.Nodes[0].Nodes)
86 {
87 if (tr.Text.Equals(tvTv.SelectedNode.Text))
88 {
89 MessageBox.Show("已有该频道");
90 return;
91 }
92 }
93 tvTv.Nodes[0].Nodes.Add(tvTv.SelectedNode.Text);
94 //展开“我的电视台”的节点
95 tvTv.Nodes[0].Expand();
96 }
97 /// <summary>
98 /// 右键删除
99 /// </summary>
100 /// <param name="sender"></param>
101 /// <param name="e"></param>
102 private void tmsiDelete_Click(object sender, EventArgs e)
103 {
104 tvTv.SelectedNode.Remove();
105 }
106 /// <summary>
107 /// 选中节点后
108 /// </summary>
109 /// <param name="sender"></param>
110 /// <param name="e"></param>
111 private void tvTv_AfterSelect(object sender, TreeViewEventArgs e)
112 {
113 BangChannel();
114 }
115 public void BangChannel()
116 {
117 //选中根节点不显示节目
118 if (tvTv.SelectedNode.Level == 0)
119 {
120 dgvProgram.DataSource = null;
121 return;
122 }
123 //用选中的频道找节目列表
124 dgvProgram.DataSource = clm.channelBase[tvTv.SelectedNode.Text].ProgramList;
125 }
126
127 }
128 }
-------------窗体