一.框架
二.主窗体
三.进入代码阶段
1.xml文件
①.FullChannels.xml
<?xml version="1.0" encoding="utf-8" ?> <TVChannels> <Channel> <channelType>TypeA</channelType> <!--频道类型--> <tvChannel>北京电视台</tvChannel> <!--频道名称--> <path>files/北京电视台.xml</path> <!--频道对应XML文件的本地路径--> </Channel> <Channel> <channelType>TypeB</channelType> <tvChannel>凤凰卫视</tvChannel> <path>files/凤凰卫视.xml</path> </Channel> </TVChannels>
②.北京电视台.xml
<?xml version="1.0" encoding="utf-8" ?> <typeA version =" 1.0"> <channelName>北京电视台</channelName> <tvProgramTable> <tvProgram> <playTime>2013-9-29 16:19</playTime> <meridien>下午档</meridien> <programName>《玫瑰剧场》55集剧:闯关东中篇(24)</programName> <path>**</path> </tvProgram> <tvProgram> <playTime>2013-9-29 18:00</playTime> <meridien>晚间档</meridien> <programName>新闻晚高峰</programName> <path>**</path> </tvProgram> <tvProgram> <playTime>2013-9-29 18:30</playTime> <meridien>晚间档</meridien> <programName>北京新闻</programName> <path>**</path> </tvProgram> <tvProgram> <playTime>2013-9-29 18:56</playTime> <meridien>晚间档</meridien> <programName>天气预报</programName> <path>**</path> </tvProgram> <tvProgram> <playTime>2013-9-29 19:00</playTime> <meridien>晚间档</meridien> <programName>转播CCTV新闻联播</programName> <path>**</path> </tvProgram> <tvProgram> <playTime>2013-9-29 19:31</playTime> <meridien>晚间档</meridien> <programName>看气象</programName> <path>**</path> </tvProgram> <tvProgram> <playTime>2013-9-29 19:40</playTime> <meridien>晚间档</meridien> <programName>专题片:我爱你,中国(15)</programName> <path>**</path> </tvProgram> <tvProgram> <playTime>2013-9-29 20:22</playTime> <meridien>晚间档</meridien> <programName>《爸妈都是老党员》首播庆典</programName> <path>**</path> </tvProgram> <tvProgram> <playTime>2013-9-29 21:21</playTime> <meridien>晚间档</meridien> <programName>《红星剧场》38集剧:咱爸咱妈六十年(1)</programName> <path>**</path> </tvProgram> <tvProgram> <playTime>2013-9-29 23:25</playTime> <meridien>晚间档</meridien> <programName>重播文艺:花样年华歌舞大赛(5)</programName> <path>**</path> </tvProgram> </tvProgramTable> </typeA>
③.凤凰卫视.xml
<?xml version="1.0" encoding="utf-8" ?> <typeB version =" 1.0"> <ProgramList> <Program> <playTime>2013-9-29 01:30</playTime> <!--播出时间--> <name>金龙鱼凤凰剧场:李算(9)</name> <!--节目名称--> <path>**</path> <!--节目视频的本地路径--> </Program> <Program> <playTime>2013-9-29 05:30</playTime> <!--播出时间--> <name>别巡检调查队(34)(R) </name> <!--节目名称--> <path>**</path> <!--节目视频的本地路径--> </Program> </ProgramList> </typeB>
2.编写类
①.TVprogram.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Day_网络_电视精灵 { public class TVprogram { //时间 public DateTime PlayTime { get; set; } //时段 public string Meridien { get; set; } //节目名称 public string ProgramName { get; set; } //视频路径 public string Path { get; set; } } }
②.ChannelBase.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Day_网络_电视精灵 { //频道父类 public abstract class ChannelBase { 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 Cb(); } }
③.ChannelManager.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; namespace Day_网络_电视精灵 { //频道管理类 public class ChannelManager { public List<ChannelBase> list = new List<ChannelBase>(); public void ResolveChannel() { XmlDocument xd=new XmlDocument(); xd.Load("FullChannels.xml"); XmlNode root = xd.DocumentElement; foreach (XmlNode item in root.ChildNodes) { ChannelBase channel = Factory.Fc(item["channelType"].InnerText); channel.Type = item["channelType"].InnerText; channel.ChannelName = item["tvChannel"].InnerText; channel.Path = item["path"].InnerText; list.Add(channel); } } } }
④.Factory.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Day_网络_电视精灵 { public class Factory { //创建一个工厂类 public static ChannelBase Fc(string type) { ChannelBase types = null; switch (type) { case "TypeA": types = new TypeA(); break; case "TypeB": types=new TypeB(); break; } return types; } } }
⑤.TypeA.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; namespace Day_网络_电视精灵 { public class TypeA:ChannelBase { public override void Cb() { XmlDocument xd = new XmlDocument(); 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); } } } } } }
⑥.TypeB.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; namespace Day_网络_电视精灵 { public class TypeB:ChannelBase { public override void Cb() { XmlDocument xd = new XmlDocument(); 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); } } } } } }
⑦.FrmMain.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Day_网络_电视精灵 { public partial class FrmMain : Form { public FrmMain() { InitializeComponent(); } public ChannelManager manager = new ChannelManager(); private void FrmMain_Load(object sender, EventArgs e) { //添加根节点 dataGridView1.AutoGenerateColumns = false; TreeNode node=new TreeNode(); node.Text = "我的电视台"; treeView1.Nodes.Add(node); TreeNode nodee = new TreeNode(); nodee.Text = "所有电视台"; treeView1.Nodes.Add(nodee); manager.ResolveChannel(); List<ChannelBase> list = manager.list; int num = 0; foreach (ChannelBase 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= treeView1.SelectedNode; ChannelBase channel= manager.list[Convert.ToInt16(tn.Tag)]; channel.ProgremList.Clear(); channel.Cb(); List<TVprogram> list= channel.ProgremList; dataGridView1.DataSource = list; } } }
三.接下来就可以看电视啦!!!!!!1!!!!
四.大功告成!!!!!!!!!!