//定义一个频道类 namespace 网络电视精灵2 { public class Tvprogeam { public string playTime { get; set; } public string meridien { get; set; } public string programName{ get; set; } public string path { get; set; } } }
定义频道父类
public abstract class ChannelBase
{
public string type{ get; set; }
public string channeName { get; set; }
public string Path{ get; set; }
//定义一个频道类的集合,用来存储两个频道;
public List<Tvprogeam>tv{get;set;}
public abstract void fetch();
}
定义一个工厂类
public class ChannelFactory
{
public static ChannelBase CreateChannel(string type)
{
ChannelBase channel = null;
switch(type)
{
case"TypeA":
channel = new typeAChannel();
break;
case "TypeB":
channel = new TypeBChannel();
break;
}
return channel;
}
//从XML文件中遍历出,然后存入到list集合中
public class ChannlManager
{
public List<ChannelBase> FullChannels{ get; set; }
public ChannlManager()
{
FullChannels = new List<ChannelBase>();
}
public void Readxml()
{
XmlDocument doc = new XmlDocument();
doc.Load("FullChannels.xml");
XmlNode rood = doc.DocumentElement;
foreach (XmlNode item in rood.ChildNodes)
{
string type = item["channelType"].InnerText;
ChannelBase channel = ChannelFactory.CreateChannel(type);
channel.channeName= item["tvChannel"].InnerText;
channel.Path= item["path"].InnerText;
FullChannels.Add(channel);
}
}
}
//遍历出凤凰卫视xml文件,将其存放在list集合中
public class TypeBChannel:ChannelBase
{
public static List<Tvprogeam> list = new List<Tvprogeam>();
public override void fetch()
{
if (tv == null)
{
tv = new List<Tvprogeam>();
}
XmlDocument doc = new XmlDocument();
doc.Load("凤凰卫视.xml");
XmlNode root = doc.DocumentElement;
foreach (XmlNode item in root.ChildNodes)
{
foreach (XmlNode item2 in item.ChildNodes)
{
Tvprogeam tv2 = new Tvprogeam();
tv2.playTime = item2["playTime"].InnerText;
tv2.meridien=item2["name"].InnerText;
tv2.path=item2["path"].InnerText;
list.Add(tv2);
}
将北京电视台xml文件遍历出然后存入list<Tvprogeam>
public static List<Tvprogeam> list = new List<Tvprogeam>();
public override void fetch()
{
if (list==null)
{
list= new List<Tvprogeam>();
}
XmlDocument doc = new XmlDocument();
doc.Load("北京电视台.xml");
XmlNode root = doc.DocumentElement;
foreach (XmlNode item in root.ChildNodes)
{
if (item.Name.Equals("tvProgramTable"))
{
foreach (XmlNode item2 in item.ChildNodes)
{
Tvprogeam tv2 = new Tvprogeam();
tv2.playTime = item2["meridien"].InnerText;
tv2.meridien=item2["programName"].InnerText;
tv2.path=item2["path"].InnerText;
list.Add(tv2);
}
}
}
}
}
private void FrmMain_Load(object sender, EventArgs e)
{
TreeNode minenode = new TreeNode();
minenode.Text = "我的电视台";
//bind
tvList.Nodes.Add(minenode);
//根节点
TreeNode root = new TreeNode();
root.Text = "所有电视台";
//bind
tvList.Nodes.Add(root);
/**0.架构搭建
*
* 1.先从xml文件读取数据到泛型集合
* *
*/
ChannelManager manager = new ChannelManager();
manager.ResolveChannels();
List<ChannelBase> list = manager.List;
foreach (ChannelBase item in list)
{
//一个item代表一个频道对象
//TreeNode
TreeNode tn = new TreeNode();
tn.Text = item.ChannelName;
tn.Tag = item;
root.Nodes.Add(tn);
}
/** *
* 2.将泛型绑定到Tv上
*
*/
}
private void tvList_AfterSelect(object sender, TreeViewEventArgs e)
{
//01.获取tv上选中的节点
if (tvList.SelectedNode.Tag != null)
{
TreeNode selectNode = tvList.SelectedNode;
ChannelBase channel = (ChannelBase)selectNode.Tag;
//给LIst集合填充数据
channel.Jx();
//
List<TvProgram> list = channel.ProgramList1;
dgvList.DataSource = list;
}
}
}
}
}
}
}