//父类频道 public abstract class ChannelBse { //频道类型 public string Type { get; set; } //频道名称 public string ChannelName { get; set; } //频道路径 public string Path { get; set; } //定义一个子类的对象 public List<TVprogram> ProgremList = new List<TVprogram>(); //解析频道节目单信息 public abstract void Fetch(); } }
//频道管理类 public class ChannelManger { public List<ChannelBse> list = new List<ChannelBse>(); public void ResolveChannel() { //创建一个xml的对象 XmlDocument xd = new XmlDocument(); //读取指定的XML文档 xd.Load("FullChannels.xml"); //读取XML的根节点 XmlNode root = xd.DocumentElement; //对子节点进行循环 foreach (XmlNode item in root.ChildNodes) { //加载频道 节目 路径。 ChannelBse channel = Factory.Fc(item["channelType"].InnerText); channel.Type = item["channelType"].InnerText; channel.ChannelName = item["tvChannel"].InnerText; channel.Path = item["path"].InnerText; list.Add(channel); } } }
public class Factory { //简单工厂类 public static ChannelBse Fc(string type) { //频道设置为空 ChannelBse types = null; //把每个节点的内容都显示出来 switch (type) { case "TypeA": types = new TypeA(); break; case "TypeB": types=new TypeA(); break; } return types; } } }
//电视节目类 public class TVprogram { //时间 public DateTime PlayTime { get; set; } //时段 public string Meridien { get; set; } //节目名称 public string ProgramName { get; set; } //视频路径 public string Path { get; set; } } }
//北京电视台类 public class TypeA :ChannelBse { public override void Fetch() { //获取频道列表 XmlDocument xd = new XmlDocument(); //加载XML文件 xd.Load("北京电视台.xml"); //获取根节点 XmlNode root = xd.DocumentElement; //循环遍历节点 foreach (XmlNode item in root.ChildNodes) { if (item.Name.Equals("tvProgramTable")) { foreach (XmlNode item2 in item.ChildNodes) { //创建一个对象 TVprogram tp = new TVprogram(); //加载所有频道列表 tp.PlayTime = Convert.ToDateTime(item2["playTime"].InnerText); tp.Meridien = item2["meridien"].InnerText; tp.ProgramName = item2["programName"].InnerText; tp.Path = item2["path"].InnerText; ProgremList.Add(tp); } } } }
//凤凰卫视类 public class TypeB:ChannelBse { public override void Fetch() { //获取频道列表 XmlDocument xd = new XmlDocument(); //加载XML文件 xd.Load("凤凰卫视.xml"); //获取根节点 XmlNode root = xd.DocumentElement; //循环遍历节点 foreach (XmlNode item in root.ChildNodes) { if (item.Name.Equals("ProgramList")) { foreach (XmlNode item2 in item.ChildNodes) { TVprogram tp = new TVprogram(); //加载所有频道列表 tp.PlayTime = Convert.ToDateTime(item2["playTime"].InnerText); tp.ProgramName = item2["name"].InnerText; tp.Path = item2["path"].InnerText; ProgremList.Add(tp); } }
主窗体代码:
//创建一个频道管理类的对象 public ChannelManger manager = new ChannelManger(); //添加根节点 private void Form1_Load(object sender, EventArgs e) { //皮肤修改 skinEngine1.SkinFile = "PageColor1.ssk"; //设置dataGridView的自动生成列为假 dataGridView1.AutoGenerateColumns = false; //创建一个节点的对象 TreeNode node = new TreeNode(); //添加一个根节点 node.Text = "我的电视台"; tvman.Nodes.Add(node); //创建一个节点的对象 TreeNode nodee = new TreeNode(); //添加一个根节点 nodee.Text = "所有电视台"; tvman.Nodes.Add(nodee); manager.ResolveChannel(); //通过索引访问父类 List<ChannelBse> list = manager.list; //定义一个空值 int num = 0; foreach (ChannelBse item in list) { TreeNode tn = new TreeNode(); tn.Text = item.ChannelName; tn.Tag = num; nodee.Nodes.Add(tn); num++; } }
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { //定义一个获取当前控件中选定的树节点 TreeNode tn = tvman.SelectedNode; // ChannelBse channel = manager.list[Convert.ToInt16(tn.Tag)]; //清除所有元素 channel.ProgremList.Clear(); //频道节目单信息 channel.Fetch(); List<TVprogram> list = channel.ProgremList; dataGridView1.DataSource = list; TreeNode selectedNode = tvman.SelectedNode; { //当前tvman控件中 选中的树节点,如果当前没有选中树节点,返回值为空 if (tvman.SelectedNode.Text == "北京电视台") { show(); } //当前tvman控件中 选中的树节点,如果当前没有选中树节点,返回值为空 else if (tvman.SelectedNode.Text == "凤凰卫视") { BFun(); } else { List<TVprogram> cc = new List<TVprogram>(); dataGridView1.DataSource = cc; } if (tvman.SelectedNode.Level == 0) { concc.Items[0].Visible = false; concc.Items[1].Visible = false; } else if (tvman.SelectedNode.Parent.Text == "我的电视台") { concc.Items[0].Visible = false; concc.Items[1].Visible = true; } else if (tvman.SelectedNode.Parent.Text == "所有电台") { concc.Items[1].Visible = false; concc.Items[0].Visible = true; } } }
// 加载北京电视台的方法 List<TVprogram> abc = new List<TVprogram>(); public void show() { XmlDocument doc = new XmlDocument(); doc.Load("北京电视台.xml"); XmlNode root = doc.DocumentElement; if (abc == null) { abc = new List<TVprogram>(); } foreach (XmlNode item in root.ChildNodes) { if (item.Name == "tvProgramTable") { foreach (XmlNode child in item.ChildNodes) { //创建子类对象 TVprogram tv = new TVprogram(); //获取子类的值 tv.PlayTime = DateTime.Parse(child["playTime"].InnerText); tv.Meridien = child["meridien"].InnerText; tv.ProgramName = child["programName"].InnerText; tv.Path = child["path"].InnerText; abc.Add(tv); } //绑定数据 dataGridView1.DataSource = abc; } } }
//加载凤凰卫视节目的方法 List<TVprogram> BList = new List<TVprogram>(); public void BFun() { XmlDocument doc = new XmlDocument(); doc.Load("凤凰卫视.xml"); XmlNode root = doc.DocumentElement; if (BList == null) { BList = new List<TVprogram>(); } foreach (XmlNode item in root.ChildNodes) { if (item.Name == "ProgramList") { foreach (XmlNode child in item.ChildNodes) { TVprogram tv = new TVprogram(); tv.PlayTime = DateTime.Parse(child["playTime"].InnerText); tv.ProgramName = child["name"].InnerText; tv.Path = child["path"].InnerText; BList.Add(tv); } } dataGridView1.DataSource = BList; } }
private void 添加到我的电视台ToolStripMenuItem_Click(object sender, EventArgs e) { foreach (TreeNode item in this.tvman.Nodes[0].Nodes) { if (item.Text == tvman.SelectedNode.Text) { MessageBox.Show("有该频道"); return; } } this.tvman.Nodes[0].Nodes.Add(tvman.SelectedNode.Text); }
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) { foreach (TreeNode item in this.tvman.Nodes[0].Nodes) { if (item.Text == tvman.SelectedNode.Text) { tvman.SelectedNode.Remove(); return; } } }