关于《网络电视精灵》项目
网络电视精灵代码详细步骤
1.定义频道以及节目的属性类
public abstract class PD { public DateTime PlayTime { get; set; } public string Name { get; set; } public string Type { get; set; } public string Path { get; set; } public abstract void F(); }
2.定义频道的北京电视台类和凤凰卫视类
北京电视台类:
public class TvA:PD { public override void F() { XmlDocument doc = new XmlDocument(); doc.Load("北京电视台.xml"); XmlNode root = doc.DocumentElement; foreach (XmlNode item in root.ChildNodes) { if (item.Name=="tvProgramTable") { foreach (XmlNode i in item.ChildNodes) { TvA t = new TvA(); t.PlayTime = Convert.ToDateTime( i["playTime"].InnerText); t.Name = i["programName"].InnerText; t.Path = i["path"].InnerText; t.Type = i["meridien"].InnerText; Tv.list1.Add(t); } } } } }
凤凰卫视类:
public class TvB:PD { public override void F() { XmlDocument doc = new XmlDocument(); doc.Load("凤凰卫视.xml"); XmlNode root = doc.DocumentElement; foreach (XmlNode item in root.ChildNodes) { if (item.Name == "ProgramList") { foreach (XmlNode i in item.ChildNodes) { TvA t = new TvA(); t.PlayTime = Convert.ToDateTime(i["playTime"].InnerText); t.Name = i["name"].InnerText; t.Path = i["path"].InnerText; Tv.list1.Add(t); } } } } }
3.定义简单工厂类
public class GC { public static PD G(string type1) { PD a = null; switch (type1) { case "TypeA": a=new TvA(); break; case "TypeB": a = new TvB(); break; } return a; } }
4.将FullChannels.xml的Xml文件数据解析到内存的集合中
public class Tv { public static List<PD> list = new List<PD>(); public static List<PD> list1 = new List<PD>(); public void F() { XmlDocument Doc = new XmlDocument(); Doc.Load("FullChannels.xml"); XmlNode root = Doc.DocumentElement; foreach (XmlNode item in root.ChildNodes) { PD p = GC.G(item["channelType"].InnerText); p.Type = item["channelType"].InnerText; p.Name = item["tvChannel"].InnerText; p.Path = item["path"].InnerText; list.Add(p); } } }
5.在Load世界动态添加treeView
List<PD> list = new List<PD>(); TreeNode root1; private void Form1_Load(object sender, EventArgs e) { root1 = new TreeNode("我喜欢的节目"); TreeNode root2 = new TreeNode("所有节目"); treeView1.Nodes.Add(root1); treeView1.Nodes.Add(root2); Tv t = new Tv(); t.F(); list = Tv.list; foreach (PD item in list) { TreeNode tn = new TreeNode(); tn.Text = item.Name; tn.Tag = item; root2.Nodes.Add(tn); } }
6.选中treeView产生的事件
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { TreeNode tn = treeView1.SelectedNode; PD p = (PD)tn.Tag; p.F(); list1 = Tv.list1; dataGridView1.DataSource = list1; }
7.右键删除添加
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) { TreeNode tn = treeView1.SelectedNode; tn.Remove(); } private void 加入我喜欢ToolStripMenuItem_Click(object sender, EventArgs e) { TreeNode tn = treeView1.SelectedNode; TreeNode tn1 = new TreeNode(); tn1.Text = tn.Text; tn1.Tag = tn.Tag; root1.Nodes.Add(tn1); }