ChannelBas类using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace TVXmlRead
{
public abstract class ChannelBase
{
#region 属性
/// <summary>
/// 频道名称
/// </summary>
private string channelName;
public string ChannelName
{
get { return channelName; }
set { channelName = value; }
}
/// <summary>
/// 频道路径
/// </summary>
private string path;
public string Path
{
get { return path; }
set { path = value; }
}
/// <summary>
/// 节目列表
/// </summary>
private List<TvProgram> programList;
public List< TvProgram> ProgramList
{
get { return programList; }
set { this.programList = value; }
}
#endregion
public abstract void Fetch();
}
}
ChannelManager类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Windows.Forms;
namespace TVXmlRead
{
public class ChannelManager
{
/// <summary>
/// 频道文件路径
/// </summary>
private string channelPath ="files/FullChannels.xml";
/// <summary>
/// 全部频道
/// </summary>
private Dictionary<string, ChannelBase> fullChannel=new Dictionary<string,ChannelBase>();
public Dictionary<string, ChannelBase> FullChannel
{
get
{
return fullChannel;
}
}
//持久化信息
private SavingInfo seria = new SavingInfo();
public SavingInfo Seria
{
get { return seria; }
}
/// <summary>
/// 启动程序时,读取FullChannels.xml,加载所有频道列表
/// </summary>
public void LoadChannel()
{
//预处理集合,防止被重复加载数据
try
{
fullChannel.Clear();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(channelPath);
XmlElement xmlRoot = xmlDoc.DocumentElement;
foreach (XmlNode node in xmlRoot.ChildNodes)
{
//根据频道类型创建对象
ChannelBase channel =CreateChannel(node["channelType"].InnerText);
channel.ChannelName = node["tvChannel"].InnerText;
channel.Path = node["path"].InnerText;
this.fullChannel.Add(channel.ChannelName, channel);
}
}
catch
{
throw new Exception("数据加载错误!");
}
}
public ChannelBase CreateChannel(string type)
{
ChannelBase channel = null;
switch (type)
{
case "TypeA":
channel = new TypeAChannel();
break;
case "TypeB":
channel = new TypeBChannel();
break;
default:
break;
}
return channel;
}
//保存定制频道信息的文本文件名称
private string saveFileName = @"files\save";
/// <summary>
/// 将我的电台信息存储到文本文件之中
/// 要解决中文乱码问题
/// </summary>
public void SaveAsTxt()
{
try
{
FileStream fs = new FileStream(saveFileName + ".txt", FileMode.Create);
StreamWriter writer = new StreamWriter(fs, Encoding.GetEncoding("GB2312"));
string type = "";
for (int index = 0; index < this.seria.MyFavor.Count; index++)
{
ChannelBase channel = this.seria.MyFavor[index];
if (channel is TypeBChannel)
{
type = "TypeB";
}
else
{
type = "TypeA";
}
writer.WriteLine(type
+ "|" + channel.ChannelName
+ "|" + channel.Path);
}
writer.WriteLine("End of my Favor");
writer.Close();
fs.Close();
}
catch (Exception ex)
{
MessageBox.Show("写入文件失败:" + ex.ToString());
}
}
/// <summary>
/// 从文本文件之中读取"我的电台"信息
/// </summary>
public void LoadFromText()
{
//#region 存在性判断
if (!File.Exists(@"files\save.txt"))
{//不存在则不再处理
return;
}
//#endregion
try
{
StreamReader reader = new StreamReader(saveFileName + ".txt", Encoding.GetEncoding("GB2312"));
string line = reader.ReadLine();
string[] propertyValues;
ChannelBase channel = null;
while (line.Trim() != "End of my Favor")
{
propertyValues = line.Split('|');
channel = CreateChannel(propertyValues[0]);
channel.ChannelName = propertyValues[1];
channel.Path = propertyValues[2];
this.seria.MyFavor.Add(channel);
line = reader.ReadLine();
}
reader.Close();
}
catch (Exception ex)
{
MessageBox.Show("文件操作异常:" + ex.Message);
}
}
}
}
SavingInfo类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TVXmlRead
{
//持久化信息类
public class SavingInfo
{
public SavingInfo()
{
this.myFavor = new List<ChannelBase>();
}
#region 属性
/// <summary>
/// 我的收藏
/// </summary>
private List<ChannelBase> myFavor;
public List<ChannelBase> MyFavor
{
get
{
if (myFavor == null)
{
return new List<ChannelBase>();
}
else
{
return myFavor;
}
}
set { myFavor = value; }
}
#endregion
}
}
TvProgram类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TVXmlRead
{
/// <summary>
/// 节目对象
/// </summary>
public class TvProgram
{
#region 属性
/// <summary>
/// 播出时间
/// </summary>
private DateTime playTime;
public DateTime PlayTime
{
get { return playTime; }
set { playTime = value; }
}
/// <summary>
/// 时段
/// </summary>
private string median;
public string Median
{
get { return median; }
set { median = value; }
}
/// <summary>
/// 名称
/// </summary>
private string programName;
public string ProgramName
{
get { return programName; }
set { programName = value; }
}
/// <summary>
/// 文件路径
/// </summary>
private string filePath;
public string FilePath
{
get { return filePath; }
set { filePath = value; }
}
#endregion
}
}
TypeAChannel类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace TVXmlRead
{
/// <summary>
/// TypeA类型节目单解析
/// </summary>
public class TypeAChannel:ChannelBase
{
/// <summary>
/// 多态,覆盖父类的获取频道列表方法
/// </summary>
public override void Fetch()
{
//获取频道列表
XmlDocument xmlDoc = new XmlDocument();
try
{
//加载频道节目单文件
xmlDoc.Load(Path);
}
catch (Exception ex)
{
Console.WriteLine("文件加载失败:" + ex.Message);
return;
}
XmlElement elem = xmlDoc.DocumentElement;
if (ProgramList == null)
{
ProgramList = new List<TvProgram>();
}
foreach (XmlNode node in elem.ChildNodes)
{
if (node.Name == "tvProgramTable")
{
foreach (XmlNode subnode in node.ChildNodes)
{
TvProgram program = new TvProgram();
program.PlayTime = DateTime.Parse(subnode["playTime"].InnerText);
program.Median = subnode["meridien"].InnerText;
program.ProgramName = subnode["programName"].InnerText;
program.FilePath = subnode["path"].InnerText;
this.ProgramList.Add(program);
}
}
}
}
}
}
TypeBChannel类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace TVXmlRead
{
/// <summary>
/// TypeB类型节目单解析
/// </summary>
public class TypeBChannel : ChannelBase
{
/// <summary>
/// 多态,覆盖父类方法,获取TypeBChannel的节目列表
/// </summary>
public override void Fetch()
{
//获取节目列表
XmlDocument xmlDoc = new XmlDocument();
try
{
xmlDoc.Load(Path);
}
catch (Exception ex)
{
Console.WriteLine("文件加载失败:" + ex.Message);
return;
}
XmlElement elem = xmlDoc.DocumentElement;
if (ProgramList == null)
{
ProgramList = new List<TvProgram>();
}
foreach (XmlNode node in elem.ChildNodes[0])//【0】第一个节点
{
TvProgram programe = new TvProgram();
programe.PlayTime = DateTime.Parse(node["playTime"].InnerText);
programe.Median = "";
programe.ProgramName = node["name"].InnerText;
programe.FilePath = node["path"].InnerText;
this.ProgramList.Add(programe);
}
}
}
}
MainForm主窗体
转载请注明原文地址: https://ju.6miu.com/read-33624.html