C#实现简易RSS

代码如下,欢迎指正

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Xml;
using System.Collections;

namespace FinallyDemo
{
    
public partial class frmRSS : Form
    {
        
public frmRSS()
        {
            InitializeComponent();
        }
        
string url = "";
        
string xmlFilePath = "";
        XmlDocument doc 
= new XmlDocument();
        TreeNode tnroot 
= new TreeNode();
        
public void DownLoadXml(string url)
        {
            
if(File.Exists(xmlFilePath))
            {
                File.Delete(xmlFilePath);
            }
            WebClient wc 
= new WebClient();
            
try
            {
                wc.DownloadFile(url, xmlFilePath);
            }
            
catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            
        }

        
public void LoadXml()
        {
            
if(File.Exists(xmlFilePath))
            {
               
                doc.Load(xmlFilePath);
                XmlNode firstNode 
= doc.DocumentElement.FirstChild;
                XmlNode root 
= firstNode.FirstChild;
                treeView1.Nodes.Add(root.InnerText);
                tnroot 
= treeView1.Nodes[0];
            }
        }

        
class Test
        {
           
public string title;
            
public string url;
            
public Test()
            {
                
            }    
            
public Test(string title,string url)
            {
                
this.title = title;
                
this.url = url;
            }           
        }

        Dictionary
<string, Test> dictionarys = new Dictionary<string, Test>();

        
public void LoadTreeTitle()
        {
            doc.Load(xmlFilePath);
            XmlNode first 
= doc.DocumentElement.FirstChild;

            tnroot.Nodes.Clear();

            
foreach (XmlNode node in first.ChildNodes)
            {
                
if(node.Name=="item")
                {
                    Test test 
= new Test();
                    TreeNode tn 
= new TreeNode();
                    
foreach (XmlNode n in node)
                    {
                       
switch(n.Name)
                       {
                           
case "title":
                               test.title 
= n.InnerText;
                               
break;
                           
case "link":
                               test.url
=n.InnerText;
                               
break;
                       }
                    }
                    tn.Text
=test.title;
                    tnroot.Nodes.Add(tn);
                    
                    
try
                    {
                        dictionarys.Add(test.title,test);  
                    }
                    
catch (System.Exception ex)
                    {
                        
                    }                               
                    
                }
            }

        }

        
private void frmRSS_Load(object sender, EventArgs e)
        {
            
string path = "http://www.cnblogs.com/yeszlr/rss";
            
string filepaht = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase) + @"\test.xml";
            xmlFilePath 
= filepaht.Substring(6, filepaht.Length - 6);
            DownLoadXml(path);
            LoadXml();
            LoadTreeTitle();
        }

        
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            
if (treeView1.SelectedNode.Nodes.Count > 0)
            {
                LoadTreeTitle();
            }
            
else
            {
                
string title = treeView1.SelectedNode.Text;
                
foreach (Test t in dictionarys.Values)
                {
                    
if (t.title == title)
                    {
                        url 
= t.url;
                    }
                }

                
if (url != "")
                {
                    webBrowser1.Navigate(url);
                }

            }
        }
    }
}

posted @ 2014-05-25 12:03  zzg168  阅读(191)  评论(0)    收藏  举报