一.框架

二.主窗体

 三.进入代码阶段

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>
复制代码
 

③.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;
        }
    }
}
复制代码