网络精灵

本文介绍了一种网络电视节目单的解析方法,通过XML文件获取节目信息,并将这些信息转换为对象列表,以便进一步处理。文章展示了如何使用C#语言实现这一过程。
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Xml;  
  7.   
  8. namespace 网络电视  
  9. {  
  10.     public class ChannelA : ChannelBase  
  11.     {  
  12.         public override void Fetch()  
  13.         {  
  14.             XmlDocument doc = new XmlDocument();  
  15.             doc.Load(Path);  
  16.             XmlNode root = doc.DocumentElement;  
  17.   
  18.             foreach (XmlNode item in root.ChildNodes)  
  19.             {  
  20.                 //一个Item  
  21.                 if (item.Name == "tvProgramTable")  
  22.                 {  
  23.                     foreach (XmlNode child in item.ChildNodes)  
  24.                     {  
  25.                         //一个child是一个节目单对象  
  26.                         TvProgram tv = new TvProgram();  
  27.                         tv.PlayTime = Convert.ToDateTime(child["playTime"].InnerText);  
  28.                         tv.Median = child["meridien"].InnerText;  
  29.                         tv.ProgramName = child["programName"].InnerText;  
  30.                         tv.FilePath = child["path"].InnerText;  
  31.   
  32.                         ProgramList.Add(tv);  
  33.   
  34.                     }  
  35.                 }  
  36.             }  
  37.         }  
  38.   
  39.   
  40.           
  41.   
  42.     }  
  43. }  

 

 

[csharp]  view plain  copy
 
 在CODE上查看代码片派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Xml;  
  7.   
  8. namespace 网络电视  
  9. {  
  10.   public  class ChannelB:ChannelBase  
  11.     {  
  12.       public override void Fetch()  
  13.       {  
  14.           XmlDocument doc = new XmlDocument();  
  15.           doc.Load(Path);  
  16.           XmlNode root = doc.DocumentElement;  
  17.           foreach (XmlNode item in root.ChildNodes)  
  18.           {  
  19.               foreach (XmlNode child in item.ChildNodes)  
  20.               {  
  21.                   TvProgram tp = new TvProgram();  
  22.                   tp.PlayTime = Convert.ToDateTime(child["playTime"].InnerText);  
  23.                   tp.ProgramName = child["name"].InnerText;  
  24.                   tp.FilePath = child["path"].InnerText;  
  25.   
  26.                   ProgramList.Add(tp);  
  27.               }  
  28.           }  
  29.       }    
  30.   
  31.        
  32.     }  
  33. }  
  34.   
  35.   
  36.       

 

[csharp]  view plain  copy
 
 在CODE上查看代码片派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using  System.Xml;  
  7. namespace 网络电视  
  8. {  
  9.   public   abstract class ChannelBase  
  10.     {  
  11.         public ChannelBase()  
  12.         {  
  13.             programList = new List<TvProgram>();  
  14.         }  
  15.  
  16.         #region 属性  
  17.   
  18.   
  19.           
  20.         private string channelName;  
  21.         public string ChannelName  
  22.         {  
  23.             get { return channelName; }  
  24.             set { channelName = value; }  
  25.         }  
  26.          
  27.         private string path;  
  28.         public string Path  
  29.         {  
  30.             get { return path; }  
  31.             set { path = value; }  
  32.         }  
  33.          
  34.         private List<TvProgram> programList;  
  35.         public List<TvProgram> ProgramList  
  36.         {  
  37.             get { return programList; }  
  38.             set { this.programList = value; }  
  39.         }  
  40.         #endregion  
  41.   
  42.         //Fetch,读取频道的xml文件  
  43.         public abstract void Fetch();  
  44.     }  
  45. }  

 

[csharp]  view plain  copy
 
 在CODE上查看代码片派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Diagnostics;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7.   
  8. namespace 网络电视  
  9. {  
  10.  public   class ChannelFactory  
  11.     {  
  12.   
  13.        
  14.         public static ChannelBase CreatChannel(string type)  
  15.         {  
  16.             ChannelBase channel = null;  
  17.             switch (type)  
  18.             {  
  19.   
  20.                 case "TypeA":  
  21.                     channel = new ChannelA();  
  22.                     break;  
  23.                 case "TypeB":  
  24.                     channel = new ChannelB();  
  25.                     break;  
  26.   
  27.   
  28.             }  
  29.             return channel;  
  30.         }  
  31.   
  32.   
  33.       
  34.     }  
  35. }  

 

[csharp]  view plain  copy
 
 在CODE上查看代码片派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Xml;  
  7.   
  8. namespace 网络电视  
  9. {  
  10.   public  class ChannelManager  
  11.     {  
  12.         private Dictionary<string, ChannelBase> fullChannels = null;  
  13.   
  14.         public Dictionary<string, ChannelBase> FullChannels  
  15.         {  
  16.             get { return fullChannels; }  
  17.             set { fullChannels = value; }  
  18.         }  
  19.   
  20.         public ChannelManager()  
  21.         {  
  22.             fullChannels=new Dictionary<string, ChannelBase>();  
  23.         }  
  24.   
  25.         public void ChangeXmlToList()  
  26.         {  
  27.            XmlDocument doc=new XmlDocument();  
  28.             doc.Load("files/FullChannels.xml");  
  29.             XmlNode root = doc.DocumentElement;  
  30.             foreach (XmlNode item in root.ChildNodes)  
  31.             {  
  32.                 string type = item["channelType"].InnerText;  
  33.                 ChannelBase channel = ChannelFactory.CreatChannel(type);  
  34.                 channel.ChannelName = item["tvChannel"].InnerText;  
  35.                 channel.Path = item["path"].InnerText;  
  36.                 fullChannels.Add(channel.ChannelName, channel);  
  37.   
  38.   
  39.             }  
  40.         }  
  41.     }  
  42. }  

 

[csharp]  view plain  copy
 
 在CODE上查看代码片派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace 网络电视  
  8. {  
  9.   public  class TvProgram  
  10.     {  
  11.          private DateTime playTime;  
  12.         public DateTime PlayTime  
  13.         {  
  14.             get { return playTime; }  
  15.             set { playTime = value; }  
  16.         }  
  17.         /// <summary>  
  18.         /// 时段  
  19.         /// </summary>  
  20.         private string median;  
  21.         public string Median  
  22.         {  
  23.             get { return median; }  
  24.             set { median = value; }  
  25.         }  
  26.          
  27.         private string programName;  
  28.         public string ProgramName  
  29.         {  
  30.             get { return programName; }  
  31.             set { programName = value; }  
  32.         }  
  33.          
  34.         private string filePath;  
  35.          public string FilePath  
  36.         {  
  37.             get { return filePath; }  
  38.             set { filePath = value; }  
  39.         }  
  40.          
  41.     }  
  42. }  


转载于:https://www.cnblogs.com/wangbenqing/p/6607502.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值