public class TypeB : ChannelBase { public override void Jx() { XmlDocument doc = new XmlDocument(); doc.Load("files/凤凰卫视.xml"); XmlNode root = doc.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.Path = item2["path"].InnerText; Tp.ProgramName = item2["name"].InnerText; ProgramList1.Add(Tp); } } } } } }
/// <summary> /// 频道管理类 /// </summary> public class ChannelManager { private List<ChannelBase> list = new List<ChannelBase>(); public List<ChannelBase> List { get { return list; } set { list = value; } } public void ResolveChannels() { //1.1 我将解析好的Data中转存放到泛型集合 //List<自定义类> XmlDocument doc = new XmlDocument(); doc.Load("files/FullChannels.xml"); //找根节点 () XmlNode root=doc.DocumentElement; foreach (XmlNode item in root.ChildNodes) { //一个item代表什么 Channel ChannelBase channel = Factory.CB(item["channelType"].InnerText); channel.ChannelType = item["channelType"].InnerText; channel.ChannelName = item["tvChannel"].InnerText; channel.Path = item["path"].InnerText; //归属到一个集合中 list.Add(channel); } } } }
/// <summary> /// 频道父类 /// </summary> public abstract class ChannelBase { //频道类型 public string ChannelType { get; set; } //channelName public string ChannelName { get; set; } //path public string Path { get; set; } //节目列表 private List<TvProgram> ProgramList = new List<TvProgram>(); public List<TvProgram> ProgramList1 { get { return ProgramList; } set { ProgramList = value; } } public abstract void Jx(); } }
public class Factory { //创建一个工厂类 public static ChannelBase CB(string type) { ChannelBase types = null; switch(type) { case"TypeA": types = new TypeA(); break; case"TypeB": types = new TypeB(); break; } return types; } } }
static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new FrmMain()); } } }
/// <summary> /// 电视节目类 /// </summary> public class TvProgram { //节目名称 public string ProgramName { get; set; } //时段 public string Meridien { get; set; } //节目路径 public string Path { get; set; } //时间 public DateTime PlayTime { get; set; } } }
public class TypeA:ChannelBase { public override void Jx() { XmlDocument doc = new XmlDocument(); doc.Load("files/北京电视台.xml"); XmlNode root = doc.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.Path = item2["path"].InnerText; Tp.ProgramName = item2["programName"].InnerText; ProgramList1.Add(Tp); } } } } } }