OpmlHelper.cs
上传用户:xtyqhl
上传日期:2022-06-07
资源大小:212k
文件大小:16k
源码类别:

Windows Mobile

开发平台:

Windows_Unix

  1. /****************************************************************
  2.  ** 文件名:OpmlHelper.cs
  3.  ** 创建人:李万龙
  4.  ** 联  系:shenlongju@hotmail.com
  5.  ** 日  期:2008-05-05
  6.  ** 描  述:操作Opml文件的方法类
  7.  ** 修改人:
  8.  ** 日  期:
  9.  ** 描  述:
  10.  ** 版  本:1.0.0.0
  11.  ** Copyright (c) 2007-2008 3ESoft
  12. ******************************************************************/
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Text;
  16. using System.Windows.Forms;
  17. using System.Xml;
  18. using System.IO;
  19. namespace _3ESoft.WindowsMobile.RSS
  20. {
  21.     /// <summary>
  22.     /// 操作Opml文件的类
  23.     /// </summary>
  24.     public class OpmlHelper
  25.     {
  26.         /// <summary>
  27.         /// 增加频道分类
  28.         /// </summary>
  29.         /// <param name="strTitle"></param>
  30.         /// <returns></returns>
  31.         public static bool ChannelAdd(string strTitle)
  32.         {
  33.             XmlDocument doc = new XmlDocument();
  34.             try
  35.             {
  36.                 doc.Load(Global.AppRssConfigFilePath() + "RSSMenu.opml");
  37.                 XmlNode xNode = doc.SelectSingleNode("/opml/body/outline").Clone();
  38.                 xNode.InnerXml = "";
  39.                 xNode.Attributes.GetNamedItem("title").InnerText = strTitle;
  40.                 if (xNode.Attributes.GetNamedItem("text") != null)
  41.                     xNode.Attributes.GetNamedItem("text").InnerText = strTitle;
  42.                 xNode.Attributes.GetNamedItem("type").InnerText = "channel";
  43.                 xNode.Attributes.GetNamedItem("id").InnerText = DateTime.Now.Ticks.ToString();
  44.                 XmlNode Node = doc.SelectSingleNode("/opml/body");
  45.                 Node.AppendChild(xNode);
  46.                 doc.Save(Global.AppRssConfigFilePath() + "RSSMenu.opml");
  47.             }
  48.             catch
  49.             { return false; }
  50.             return true;
  51.         }
  52.         /// <summary>
  53.         /// 删除频道分类
  54.         /// </summary>
  55.         /// <param name="strTitle"></param>
  56.         /// <returns></returns>
  57.         public static bool ChannelDel(string strTitle)
  58.         {
  59.             XmlDocument doc = new XmlDocument();
  60.             try
  61.             {
  62.                 doc.Load(Global.AppRssConfigFilePath() + "RSSMenu.opml");
  63.                 XmlNodeList Nodelst = doc.SelectNodes("/opml/body/outline");
  64.                 foreach (XmlNode node in Nodelst)
  65.                 {
  66.                     if (node.Attributes.GetNamedItem("title").InnerText == strTitle)
  67.                     {
  68.                         doc.RemoveChild(node);
  69.                         break;
  70.                     }
  71.                 }
  72.                 doc.Save(Global.AppRssConfigFilePath() + "RSSMenu.opml");
  73.             }
  74.             catch
  75.             {
  76.                 return false;
  77.             }
  78.             return true;
  79.         }
  80.         /// <summary>
  81.         /// 重命名分类
  82.         /// </summary>
  83.         /// <param name="strOldName"></param>
  84.         /// <param name="strNewName"></param>
  85.         /// <returns></returns>
  86.         public static bool ChannelReName(string strOldName, string strNewName)
  87.         {
  88.             XmlDocument doc = new XmlDocument();
  89.             try
  90.             {
  91.                 doc.Load(Global.AppRssConfigFilePath() + "RSSMenu.opml");
  92.                 XmlNodeList Nodelst = doc.SelectNodes("/opml/body/outline");
  93.                 foreach (XmlNode node in Nodelst)
  94.                 {
  95.                     if (node.Attributes.GetNamedItem("title").InnerText == strOldName)
  96.                     {
  97.                         node.Attributes.GetNamedItem("title").InnerText = strNewName;
  98.                         if (node.Attributes.GetNamedItem("text") != null)
  99.                             node.Attributes.GetNamedItem("text").InnerText = strNewName;
  100.                         break;
  101.                     }
  102.                 }
  103.                 doc.Save(Global.AppRssConfigFilePath() + "RSSMenu.opml");
  104.             }
  105.             catch
  106.             {
  107.                 return false;
  108.             }
  109.             return true;
  110.         }
  111.         /// <summary>
  112.         /// 添加RSS频道
  113.         /// </summary>
  114.         /// <param name="strChannel"></param>
  115.         /// <param name="opml"></param>
  116.         /// <returns></returns>
  117.         public static bool RSSAdd(string strChannel, Opml opml)
  118.         {
  119.             XmlDocument doc = new XmlDocument();
  120.             try
  121.             {
  122.                 doc.Load(Global.AppRssConfigFilePath() + "RSSMenu.opml");
  123.                 XmlNodeList Nodelst = doc.SelectNodes("/opml/body/outline");
  124.                 foreach (XmlNode node in Nodelst)
  125.                 {
  126.                     if (node.Attributes.GetNamedItem("title").InnerText == strChannel)
  127.                     {
  128.                         XmlNode nodeOpml = doc.SelectSingleNode("/opml/body/outline/outline").Clone();
  129.                         nodeOpml.Attributes.GetNamedItem("id").InnerText = DateTime.Now.Ticks.ToString();
  130.                         nodeOpml.Attributes.GetNamedItem("text").InnerText = opml.Text;
  131.                         nodeOpml.Attributes.GetNamedItem("title").InnerText = opml.Text;
  132.                         nodeOpml.Attributes.GetNamedItem("xmlUrl").InnerText = opml.XmlUrl;
  133.                         nodeOpml.Attributes.GetNamedItem("description").InnerText = "";
  134.                         nodeOpml.Attributes.GetNamedItem("flags").InnerText = "";
  135.                         node.AppendChild(nodeOpml);
  136.                         break;
  137.                     }
  138.                 }
  139.                 doc.Save(Global.AppRssConfigFilePath() + "RSSMenu.opml");
  140.             }
  141.             catch
  142.             {
  143.                 return false;
  144.             }
  145.             return true;
  146.         }
  147.         /// <summary>
  148.         /// 删除频道
  149.         /// </summary>
  150.         /// <param name="strChannel"></param>
  151.         /// <param name="opml"></param>
  152.         /// <returns></returns>
  153.         public static bool RSSDel(string strChannel, Opml opml)
  154.         {
  155.             XmlDocument doc = new XmlDocument();
  156.             try
  157.             {
  158.                 doc.Load(Global.AppRssConfigFilePath() + "RSSMenu.opml");
  159.                 XmlNodeList Nodelst = doc.SelectNodes("/opml/body/outline");
  160.                 bool finished = false;
  161.                 foreach (XmlNode node in Nodelst)
  162.                 {
  163.                     if (node.Attributes.GetNamedItem("title").InnerText == strChannel)
  164.                     {
  165.                         foreach (XmlNode nodeOpml in node.ChildNodes)
  166.                         {
  167.                             if (nodeOpml.Attributes.GetNamedItem("text").InnerText == opml.Text)
  168.                             {
  169.                                 node.RemoveChild(nodeOpml);
  170.                                 finished = true;
  171.                                 break;
  172.                             }
  173.                         }
  174.                     }
  175.                     if (finished)
  176.                         break;
  177.                 }
  178.                 doc.Save(Global.AppRssConfigFilePath() + "RSSMenu.opml");
  179.             }
  180.             catch { return false; }
  181.             return true;
  182.         }
  183.         /// <summary>
  184.         /// 编辑频道信息
  185.         /// </summary>
  186.         /// <param name="strOldChannel">原来位置</param>
  187.         /// <param name="strChannel">新位置</param>
  188.         /// <param name="oldOpml"></param>
  189.         /// <param name="opml"></param>
  190.         /// <returns></returns>
  191.         public static bool RSSEdit(string strOldChannel, string strChannel, Opml oldOpml, Opml opml)
  192.         {
  193.             if (strOldChannel == strChannel)
  194.             {
  195.                 XmlDocument doc = new XmlDocument();
  196.                 try
  197.                 {
  198.                     doc.Load(Global.AppRssConfigFilePath() + "RSSMenu.opml");
  199.                     XmlNodeList Nodelst = doc.SelectNodes("/opml/body/outline");
  200.                     bool finished = false;
  201.                     foreach (XmlNode node in Nodelst)
  202.                     {
  203.                         if (node.Attributes.GetNamedItem("title").InnerText == strChannel)
  204.                         {
  205.                             foreach (XmlNode nodeOpml in node.ChildNodes)
  206.                             {
  207.                                 if (nodeOpml.Attributes.GetNamedItem("text").InnerText == oldOpml.Text)
  208.                                 {
  209.                                     nodeOpml.Attributes.GetNamedItem("text").InnerText = opml.Text;
  210.                                     nodeOpml.Attributes.GetNamedItem("xmlUrl").InnerText = opml.XmlUrl; ;
  211.                                     nodeOpml.Attributes.GetNamedItem("htmlUrl").InnerText = "";
  212.                                     nodeOpml.Attributes.GetNamedItem("description").InnerText = opml.Text;
  213.                                     finished = true;
  214.                                     break;
  215.                                 }
  216.                             }
  217.                         }
  218.                         if (finished)
  219.                             break;
  220.                     }
  221.                     doc.Save(Global.AppRssConfigFilePath() + "RSSMenu.opml");
  222.                 }
  223.                 catch { return false; }
  224.             }
  225.             else
  226.             {
  227.                 RSSDel(strOldChannel, oldOpml);
  228.                 RSSAdd(strChannel, opml);
  229.             }
  230.             return false;
  231.         }
  232.         /// <summary>
  233.         /// 清除频道本地内容文件
  234.         /// </summary>
  235.         /// <param name="strChannel"></param>
  236.         /// <param name="opml"></param>
  237.         /// <returns></returns>
  238.         public static bool RSSClear(string strChannel, Opml opml)
  239.         {
  240.             string Filename = Global.FormatUrl(opml.XmlUrl);
  241.             File.Delete(Filename);
  242.             if (!File.Exists(Filename))
  243.                 return true;
  244.             return false;
  245.         }
  246.         /// <summary>
  247.         /// 读取Opml文件绑定到TreeView上
  248.         /// </summary>
  249.         /// <param name="tvRSS"></param>
  250.         public static void ReadOpmlToTreeView(TreeView tvRSS)
  251.         {
  252.             tvRSS.Nodes.Clear();
  253.             XmlDocument doc = new XmlDocument();
  254.             string menuFile = Global.AppRssConfigFilePath() + "RSSMenu.opml";
  255.             if (!File.Exists(menuFile))
  256.             {
  257.                 //文件不存在,初始化
  258.                 return;
  259.             }
  260.             try
  261.             {
  262.                 doc.Load(menuFile);
  263.             }
  264.             catch
  265.             {
  266.                 //文件被破坏,要修复
  267.                 return;
  268.             }
  269.             XmlNodeList nodeChannelList;
  270.             nodeChannelList = doc.SelectNodes("/opml/body/outline");
  271.             foreach (XmlNode xNode in nodeChannelList)
  272.             {
  273.                 if (xNode.Attributes.GetNamedItem("title") == null || xNode.Attributes.GetNamedItem("title") == null)
  274.                 {
  275.                     //文件结构不对
  276.                     return;
  277.                 }
  278.                 RssTreeNode tvChannelNode = new RssTreeNode(xNode.Attributes.GetNamedItem("title").InnerText);
  279.                 Opml oChannel = new Opml();
  280.                 oChannel.ChannelTitle = tvChannelNode.Text;
  281.                 oChannel.Type = RssType.Channel;
  282.                 if (xNode.Attributes.GetNamedItem("id") != null)
  283.                     oChannel.ID = xNode.Attributes.GetNamedItem("id").InnerText;
  284.                 oChannel.Title = oChannel.Text = tvChannelNode.Text;
  285.                 tvChannelNode.Opml = oChannel;
  286.                 if (xNode.HasChildNodes)
  287.                 {
  288.                     XmlNodeList nodeRssList = xNode.ChildNodes;// doc.SelectNodes("/opml/body/outline/outline");
  289.                     foreach (XmlNode xChileNode in nodeRssList)
  290.                     {
  291.                         if (xChileNode.Attributes.GetNamedItem("text") == null)
  292.                         {
  293.                             //文件结构不对
  294.                             return;
  295.                         }
  296.                         RssTreeNode tvRssNode = new RssTreeNode(xChileNode.Attributes.GetNamedItem("text").InnerText);
  297.                         Opml oRss = new Opml();
  298.                         oRss.Type = RssType.Rss;
  299.                         oRss.Title=oRss.Text = tvRssNode.Text;
  300.                         
  301.                         if (xChileNode.Attributes.GetNamedItem("id") != null)
  302.                             oRss.ID = xChileNode.Attributes.GetNamedItem("id").InnerText;
  303.                         if (xChileNode.Attributes.GetNamedItem("description") != null)
  304.                             oRss.Copyright = xChileNode.Attributes.GetNamedItem("description").InnerText;
  305.                         if (xChileNode.Attributes.GetNamedItem("pubDate") != null)
  306.                             oRss.PubDate = xChileNode.Attributes.GetNamedItem("pubDate").InnerText;
  307.                         if (xChileNode.Attributes.GetNamedItem("xmlUrl") != null)
  308.                             oRss.XmlUrl = xChileNode.Attributes.GetNamedItem("xmlUrl").InnerText;
  309.                         if (xChileNode.Attributes.GetNamedItem("copyright") != null)
  310.                             oRss.Copyright = xChileNode.Attributes.GetNamedItem("copyright").InnerText;
  311.                         if (xChileNode.Attributes.GetNamedItem("htmlUrl") != null)
  312.                             oRss.PubDate = xChileNode.Attributes.GetNamedItem("htmlUrl").InnerText;
  313.                         //tvRssNode.ImageIndex = 1;
  314.                         //tvRssNode.SelectedImageIndex = 2;
  315.                         tvRssNode.Opml = oRss;
  316.                         tvChannelNode.Nodes.Add(tvRssNode);
  317.                     }
  318.                 }
  319.                 //BindOpmlNodeToTreeNode(RssTreeNode, tvNode);
  320.                 tvRSS.Nodes.Add(tvChannelNode);
  321.             }
  322.         }
  323.         private static void BindOpmlNodeToTreeNode(XmlNodeList xmlNodes, RssTreeNode tvNode)
  324.         { }
  325.         /// <summary>
  326.         /// 返回Channel列表
  327.         /// </summary>
  328.         /// <returns></returns>
  329.         public static List<OpmlChannel> GetChannelList()
  330.         {
  331.             XmlDocument doc = new XmlDocument();
  332.             string menuFile = Global.AppRssConfigFilePath() + "RSSMenu.opml";
  333.             if (!File.Exists(menuFile))
  334.             {
  335.                 //文件不存在,初始化
  336.                 return null;
  337.             }
  338.             try
  339.             {
  340.                 doc.Load(menuFile);
  341.             }
  342.             catch
  343.             {
  344.                 //文件被破坏,要修复
  345.                 return null;
  346.             }
  347.             XmlNodeList nodeChannelList;
  348.             nodeChannelList = doc.SelectNodes("/opml/body/outline");
  349.             List<OpmlChannel> oChannelList = new List<OpmlChannel>();
  350.             foreach (XmlNode xNode in nodeChannelList)
  351.             {
  352.                 OpmlChannel oChannel = new OpmlChannel();
  353.                 if (xNode.Attributes.GetNamedItem("title") != null && xNode.Attributes.GetNamedItem("id") != null)
  354.                 {
  355.                     oChannel.Title = xNode.Attributes.GetNamedItem("title").InnerText;
  356.                     oChannel.ID = xNode.Attributes.GetNamedItem("id").InnerText;
  357.                     oChannelList.Add(oChannel);
  358.                 }
  359.                 
  360.             }
  361.             return oChannelList;
  362.         }
  363.         /// <summary>
  364.         /// 返回单个Channel节点
  365.         /// </summary>
  366.         /// <param name="strChannel"></param>
  367.         /// <returns></returns>
  368.         public static XmlNode GetChannelNode(string strChannel)
  369.         {
  370.             XmlDocument doc = new XmlDocument();
  371.             string menuFile = Global.AppRssConfigFilePath() + "RSSMenu.opml";
  372.             if (!File.Exists(menuFile))
  373.             {
  374.                 //文件不存在,初始化
  375.                 return null;
  376.             }
  377.             try
  378.             {
  379.                 doc.Load(menuFile);
  380.             }
  381.             catch
  382.             {
  383.                 //文件被破坏,要修复
  384.                 return null;
  385.             }
  386.             XmlNodeList nodeChannelList;
  387.             nodeChannelList = doc.SelectNodes("/opml/body/outline");
  388.             XmlNode ChannelNode = null;
  389.             foreach (XmlNode xNode in nodeChannelList)
  390.             {
  391.                 if (xNode.Attributes.GetNamedItem("title") != null && xNode.Attributes.GetNamedItem("title").InnerText ==strChannel)
  392.                 {
  393.                     ChannelNode = xNode;
  394.                     break;                    
  395.                 }
  396.             }
  397.             return ChannelNode;
  398.         }
  399.     }
  400. }