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);
                }

            }
        }
    }
}

这只是一个小Test!自己都感觉有好多的BUG,没空修改过来,就先放这里~

转载于:https://www.cnblogs.com/yeszlr/archive/2009/12/19/1627723.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值