OpmlHelper.cs
资源名称:MobileRss.rar [点击查看]
上传用户:xtyqhl
上传日期:2022-06-07
资源大小:212k
文件大小:16k
源码类别:
Windows Mobile
开发平台:
Windows_Unix
- /****************************************************************
- ** 文件名:OpmlHelper.cs
- ** 创建人:李万龙
- ** 联 系:shenlongju@hotmail.com
- ** 日 期:2008-05-05
- ** 描 述:操作Opml文件的方法类
- ** 修改人:
- ** 日 期:
- ** 描 述:
- ** 版 本:1.0.0.0
- ** Copyright (c) 2007-2008 3ESoft
- ******************************************************************/
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Windows.Forms;
- using System.Xml;
- using System.IO;
- namespace _3ESoft.WindowsMobile.RSS
- {
- /// <summary>
- /// 操作Opml文件的类
- /// </summary>
- public class OpmlHelper
- {
- /// <summary>
- /// 增加频道分类
- /// </summary>
- /// <param name="strTitle"></param>
- /// <returns></returns>
- public static bool ChannelAdd(string strTitle)
- {
- XmlDocument doc = new XmlDocument();
- try
- {
- doc.Load(Global.AppRssConfigFilePath() + "RSSMenu.opml");
- XmlNode xNode = doc.SelectSingleNode("/opml/body/outline").Clone();
- xNode.InnerXml = "";
- xNode.Attributes.GetNamedItem("title").InnerText = strTitle;
- if (xNode.Attributes.GetNamedItem("text") != null)
- xNode.Attributes.GetNamedItem("text").InnerText = strTitle;
- xNode.Attributes.GetNamedItem("type").InnerText = "channel";
- xNode.Attributes.GetNamedItem("id").InnerText = DateTime.Now.Ticks.ToString();
- XmlNode Node = doc.SelectSingleNode("/opml/body");
- Node.AppendChild(xNode);
- doc.Save(Global.AppRssConfigFilePath() + "RSSMenu.opml");
- }
- catch
- { return false; }
- return true;
- }
- /// <summary>
- /// 删除频道分类
- /// </summary>
- /// <param name="strTitle"></param>
- /// <returns></returns>
- public static bool ChannelDel(string strTitle)
- {
- XmlDocument doc = new XmlDocument();
- try
- {
- doc.Load(Global.AppRssConfigFilePath() + "RSSMenu.opml");
- XmlNodeList Nodelst = doc.SelectNodes("/opml/body/outline");
- foreach (XmlNode node in Nodelst)
- {
- if (node.Attributes.GetNamedItem("title").InnerText == strTitle)
- {
- doc.RemoveChild(node);
- break;
- }
- }
- doc.Save(Global.AppRssConfigFilePath() + "RSSMenu.opml");
- }
- catch
- {
- return false;
- }
- return true;
- }
- /// <summary>
- /// 重命名分类
- /// </summary>
- /// <param name="strOldName"></param>
- /// <param name="strNewName"></param>
- /// <returns></returns>
- public static bool ChannelReName(string strOldName, string strNewName)
- {
- XmlDocument doc = new XmlDocument();
- try
- {
- doc.Load(Global.AppRssConfigFilePath() + "RSSMenu.opml");
- XmlNodeList Nodelst = doc.SelectNodes("/opml/body/outline");
- foreach (XmlNode node in Nodelst)
- {
- if (node.Attributes.GetNamedItem("title").InnerText == strOldName)
- {
- node.Attributes.GetNamedItem("title").InnerText = strNewName;
- if (node.Attributes.GetNamedItem("text") != null)
- node.Attributes.GetNamedItem("text").InnerText = strNewName;
- break;
- }
- }
- doc.Save(Global.AppRssConfigFilePath() + "RSSMenu.opml");
- }
- catch
- {
- return false;
- }
- return true;
- }
- /// <summary>
- /// 添加RSS频道
- /// </summary>
- /// <param name="strChannel"></param>
- /// <param name="opml"></param>
- /// <returns></returns>
- public static bool RSSAdd(string strChannel, Opml opml)
- {
- XmlDocument doc = new XmlDocument();
- try
- {
- doc.Load(Global.AppRssConfigFilePath() + "RSSMenu.opml");
- XmlNodeList Nodelst = doc.SelectNodes("/opml/body/outline");
- foreach (XmlNode node in Nodelst)
- {
- if (node.Attributes.GetNamedItem("title").InnerText == strChannel)
- {
- XmlNode nodeOpml = doc.SelectSingleNode("/opml/body/outline/outline").Clone();
- nodeOpml.Attributes.GetNamedItem("id").InnerText = DateTime.Now.Ticks.ToString();
- nodeOpml.Attributes.GetNamedItem("text").InnerText = opml.Text;
- nodeOpml.Attributes.GetNamedItem("title").InnerText = opml.Text;
- nodeOpml.Attributes.GetNamedItem("xmlUrl").InnerText = opml.XmlUrl;
- nodeOpml.Attributes.GetNamedItem("description").InnerText = "";
- nodeOpml.Attributes.GetNamedItem("flags").InnerText = "";
- node.AppendChild(nodeOpml);
- break;
- }
- }
- doc.Save(Global.AppRssConfigFilePath() + "RSSMenu.opml");
- }
- catch
- {
- return false;
- }
- return true;
- }
- /// <summary>
- /// 删除频道
- /// </summary>
- /// <param name="strChannel"></param>
- /// <param name="opml"></param>
- /// <returns></returns>
- public static bool RSSDel(string strChannel, Opml opml)
- {
- XmlDocument doc = new XmlDocument();
- try
- {
- doc.Load(Global.AppRssConfigFilePath() + "RSSMenu.opml");
- XmlNodeList Nodelst = doc.SelectNodes("/opml/body/outline");
- bool finished = false;
- foreach (XmlNode node in Nodelst)
- {
- if (node.Attributes.GetNamedItem("title").InnerText == strChannel)
- {
- foreach (XmlNode nodeOpml in node.ChildNodes)
- {
- if (nodeOpml.Attributes.GetNamedItem("text").InnerText == opml.Text)
- {
- node.RemoveChild(nodeOpml);
- finished = true;
- break;
- }
- }
- }
- if (finished)
- break;
- }
- doc.Save(Global.AppRssConfigFilePath() + "RSSMenu.opml");
- }
- catch { return false; }
- return true;
- }
- /// <summary>
- /// 编辑频道信息
- /// </summary>
- /// <param name="strOldChannel">原来位置</param>
- /// <param name="strChannel">新位置</param>
- /// <param name="oldOpml"></param>
- /// <param name="opml"></param>
- /// <returns></returns>
- public static bool RSSEdit(string strOldChannel, string strChannel, Opml oldOpml, Opml opml)
- {
- if (strOldChannel == strChannel)
- {
- XmlDocument doc = new XmlDocument();
- try
- {
- doc.Load(Global.AppRssConfigFilePath() + "RSSMenu.opml");
- XmlNodeList Nodelst = doc.SelectNodes("/opml/body/outline");
- bool finished = false;
- foreach (XmlNode node in Nodelst)
- {
- if (node.Attributes.GetNamedItem("title").InnerText == strChannel)
- {
- foreach (XmlNode nodeOpml in node.ChildNodes)
- {
- if (nodeOpml.Attributes.GetNamedItem("text").InnerText == oldOpml.Text)
- {
- nodeOpml.Attributes.GetNamedItem("text").InnerText = opml.Text;
- nodeOpml.Attributes.GetNamedItem("xmlUrl").InnerText = opml.XmlUrl; ;
- nodeOpml.Attributes.GetNamedItem("htmlUrl").InnerText = "";
- nodeOpml.Attributes.GetNamedItem("description").InnerText = opml.Text;
- finished = true;
- break;
- }
- }
- }
- if (finished)
- break;
- }
- doc.Save(Global.AppRssConfigFilePath() + "RSSMenu.opml");
- }
- catch { return false; }
- }
- else
- {
- RSSDel(strOldChannel, oldOpml);
- RSSAdd(strChannel, opml);
- }
- return false;
- }
- /// <summary>
- /// 清除频道本地内容文件
- /// </summary>
- /// <param name="strChannel"></param>
- /// <param name="opml"></param>
- /// <returns></returns>
- public static bool RSSClear(string strChannel, Opml opml)
- {
- string Filename = Global.FormatUrl(opml.XmlUrl);
- File.Delete(Filename);
- if (!File.Exists(Filename))
- return true;
- return false;
- }
- /// <summary>
- /// 读取Opml文件绑定到TreeView上
- /// </summary>
- /// <param name="tvRSS"></param>
- public static void ReadOpmlToTreeView(TreeView tvRSS)
- {
- tvRSS.Nodes.Clear();
- XmlDocument doc = new XmlDocument();
- string menuFile = Global.AppRssConfigFilePath() + "RSSMenu.opml";
- if (!File.Exists(menuFile))
- {
- //文件不存在,初始化
- return;
- }
- try
- {
- doc.Load(menuFile);
- }
- catch
- {
- //文件被破坏,要修复
- return;
- }
- XmlNodeList nodeChannelList;
- nodeChannelList = doc.SelectNodes("/opml/body/outline");
- foreach (XmlNode xNode in nodeChannelList)
- {
- if (xNode.Attributes.GetNamedItem("title") == null || xNode.Attributes.GetNamedItem("title") == null)
- {
- //文件结构不对
- return;
- }
- RssTreeNode tvChannelNode = new RssTreeNode(xNode.Attributes.GetNamedItem("title").InnerText);
- Opml oChannel = new Opml();
- oChannel.ChannelTitle = tvChannelNode.Text;
- oChannel.Type = RssType.Channel;
- if (xNode.Attributes.GetNamedItem("id") != null)
- oChannel.ID = xNode.Attributes.GetNamedItem("id").InnerText;
- oChannel.Title = oChannel.Text = tvChannelNode.Text;
- tvChannelNode.Opml = oChannel;
- if (xNode.HasChildNodes)
- {
- XmlNodeList nodeRssList = xNode.ChildNodes;// doc.SelectNodes("/opml/body/outline/outline");
- foreach (XmlNode xChileNode in nodeRssList)
- {
- if (xChileNode.Attributes.GetNamedItem("text") == null)
- {
- //文件结构不对
- return;
- }
- RssTreeNode tvRssNode = new RssTreeNode(xChileNode.Attributes.GetNamedItem("text").InnerText);
- Opml oRss = new Opml();
- oRss.Type = RssType.Rss;
- oRss.Title=oRss.Text = tvRssNode.Text;
- if (xChileNode.Attributes.GetNamedItem("id") != null)
- oRss.ID = xChileNode.Attributes.GetNamedItem("id").InnerText;
- if (xChileNode.Attributes.GetNamedItem("description") != null)
- oRss.Copyright = xChileNode.Attributes.GetNamedItem("description").InnerText;
- if (xChileNode.Attributes.GetNamedItem("pubDate") != null)
- oRss.PubDate = xChileNode.Attributes.GetNamedItem("pubDate").InnerText;
- if (xChileNode.Attributes.GetNamedItem("xmlUrl") != null)
- oRss.XmlUrl = xChileNode.Attributes.GetNamedItem("xmlUrl").InnerText;
- if (xChileNode.Attributes.GetNamedItem("copyright") != null)
- oRss.Copyright = xChileNode.Attributes.GetNamedItem("copyright").InnerText;
- if (xChileNode.Attributes.GetNamedItem("htmlUrl") != null)
- oRss.PubDate = xChileNode.Attributes.GetNamedItem("htmlUrl").InnerText;
- //tvRssNode.ImageIndex = 1;
- //tvRssNode.SelectedImageIndex = 2;
- tvRssNode.Opml = oRss;
- tvChannelNode.Nodes.Add(tvRssNode);
- }
- }
- //BindOpmlNodeToTreeNode(RssTreeNode, tvNode);
- tvRSS.Nodes.Add(tvChannelNode);
- }
- }
- private static void BindOpmlNodeToTreeNode(XmlNodeList xmlNodes, RssTreeNode tvNode)
- { }
- /// <summary>
- /// 返回Channel列表
- /// </summary>
- /// <returns></returns>
- public static List<OpmlChannel> GetChannelList()
- {
- XmlDocument doc = new XmlDocument();
- string menuFile = Global.AppRssConfigFilePath() + "RSSMenu.opml";
- if (!File.Exists(menuFile))
- {
- //文件不存在,初始化
- return null;
- }
- try
- {
- doc.Load(menuFile);
- }
- catch
- {
- //文件被破坏,要修复
- return null;
- }
- XmlNodeList nodeChannelList;
- nodeChannelList = doc.SelectNodes("/opml/body/outline");
- List<OpmlChannel> oChannelList = new List<OpmlChannel>();
- foreach (XmlNode xNode in nodeChannelList)
- {
- OpmlChannel oChannel = new OpmlChannel();
- if (xNode.Attributes.GetNamedItem("title") != null && xNode.Attributes.GetNamedItem("id") != null)
- {
- oChannel.Title = xNode.Attributes.GetNamedItem("title").InnerText;
- oChannel.ID = xNode.Attributes.GetNamedItem("id").InnerText;
- oChannelList.Add(oChannel);
- }
- }
- return oChannelList;
- }
- /// <summary>
- /// 返回单个Channel节点
- /// </summary>
- /// <param name="strChannel"></param>
- /// <returns></returns>
- public static XmlNode GetChannelNode(string strChannel)
- {
- XmlDocument doc = new XmlDocument();
- string menuFile = Global.AppRssConfigFilePath() + "RSSMenu.opml";
- if (!File.Exists(menuFile))
- {
- //文件不存在,初始化
- return null;
- }
- try
- {
- doc.Load(menuFile);
- }
- catch
- {
- //文件被破坏,要修复
- return null;
- }
- XmlNodeList nodeChannelList;
- nodeChannelList = doc.SelectNodes("/opml/body/outline");
- XmlNode ChannelNode = null;
- foreach (XmlNode xNode in nodeChannelList)
- {
- if (xNode.Attributes.GetNamedItem("title") != null && xNode.Attributes.GetNamedItem("title").InnerText ==strChannel)
- {
- ChannelNode = xNode;
- break;
- }
- }
- return ChannelNode;
- }
- }
- }