标签: C#

  • 转以调情:获得RSS 和 获得Atom

    获取XML
    1 string filePath = “temp.rss”;//指定RSS格式文件路径
    2             try
    3             {
    4                 if(this.articles == null)
    5                     this.articles = new Dictionary<string, Article>();
    6
    7                 this.articles.Clear();
    8
    9                 WebClient myClient = new WebClient();
    10                 myClient.DownloadFile(Url, filePath);
    11
    12                 XmlDocument myXml = new XmlDocument();
    13                 myXml.Load(filePath);
    14
    15                 //定位 channel 节点
    16                 XmlNode channel = myXml.DocumentElement.FirstChild; //channel node
    17
    18                 //定位 item 节点
    19                 foreach (XmlNode node in channel.ChildNodes)
    20                 {
    21                     if (node.Name == “item”)
    22                     {
    23                         Article atcl = new Article();
    24
    25                         foreach (XmlNode subNode in node.ChildNodes)
    26                         {
    27                             switch (subNode.Name)
    28                             {
    29                                 case “title”:
    30                                     atcl.Title = subNode.InnerText;
    31                                     break;
    32                                 case “link”:
    33                                     atcl.Url = subNode.InnerText;
    34                                     break;
    35                             }
    36                         }
    37
    38                         this.articles.Add(atcl.Title, atcl);
    39
    40                     }
    41                 }
    42
    43                 Clicks++;
    44
    45                 return true;
    46             }
    47             catch (Exception e)
    48             {
    49                 Console.WriteLine(e.ToString());
    50                 return false;
    51             }

    ///////////////////////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////////////////////

    获取ATOM
    1 string filePath = “temp.atom”;//Atom格式文件路径
    2             try
    3             {
    4                 if (this.articles == null)
    5                     this.articles = new Dictionary<string, Article>();
    6                 //清空文章列表泛型集合
    7                 this.articles.Clear();
    8                 //取得Atom格式文件
    9                 WebClient myClient = new WebClient();
    10                 myClient.DownloadFile(Url, filePath);
    11                 //读取XML文档
    12                 XmlDocument myXml = new XmlDocument();
    13                 myXml.Load(filePath);
    14                 //获取根节点
    15                 XmlNode feednode = myXml.DocumentElement; //feed node
    16                 //外层按文章节点entry循环
    17                 foreach (XmlNode node in feednode.ChildNodes)
    18                 {
    19                     if (node.Name == “entry”)
    20                     {
    21                         Article atcl = new Article();//实例化文章集合
    22                         //内层循环获取文章详细信息
    23                         foreach (XmlNode subNode in node.ChildNodes)
    24                         {
    25                             switch (subNode.Name)
    26                             {
    27                                 case “title”:
    28                                     atcl.Title = subNode.InnerText;
    29                                     break;
    30                                 case “link”:
    31                                     atcl.Url = subNode.Attributes[“href”].Value.ToString();
    32                                     break;
    33                             }
    34                         }
    35                         //将文章信息存入泛型集合
    36                         this.articles.Add(atcl.Title, atcl);
    37                     }
    38                 }
    39                 Clicks++;//点击次数加1
    40                 return true;
    41             }
    42             catch (Exception e)
    43             {
    44                 Console.WriteLine(e.ToString());
    45                 return false;
    46             }

    原文:http://www.cnblogs.com/nniixl/articles/1313791.html