通过解析XML文件制作的RSS阅读器

运行效果:(截取当前窗口图片快捷键:ALT+PrtSc)

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

namespace RSS阅读器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            //将指定的url页面文件下载到本地
            string path = txtPath.Text;
            WebClient wc = new WebClient();
            wc.Encoding = Encoding.UTF8;
            wc.DownloadFile(path,"rss.xml");


            XDocument xDoc = XDocument.Load("rss.xml");
            XElement root = xDoc.Root;
            TreeNode tnRoot = tvContent.Nodes.Add(root.Name.LocalName);
            //将下面的代码写成LoadRss方法
            //foreach (XElement xEle in root.Elements())
            //{
            //    TreeNode tn = tnRoot.Nodes.Add(xEle.Name.LocalName);
            //}
            LoadRss(root,tnRoot);
            
        }

        private void LoadRss(XElement ele,TreeNode tn)
        {
            foreach (XElement xEle in ele.Elements())
            {
                TreeNode tn1 = tn.Nodes.Add(xEle.Name.LocalName);
                //如果这个元素节点有子节点那么就调用递归方法,添加子节点到这个父节点上面
                if (xEle.HasElements)
                {
                    LoadRss(xEle, tn1);
                }
                else
                {
                    //如果这个元素节点没有子节点了,那么把这个元素节点的值(内容)附加到这个节点的TAG里
                    tn1.Tag = xEle.Value;
                }
            }
        }

        private void tvContent_AfterSelect(object sender, TreeViewEventArgs e)
        {

            TreeNode tn = e.Node; 
            //将TAG内容转换成字符串型的,如果转换失败返回null
            string str = tn.Tag as string; 
            if (str!=null)
            {
                webShow.DocumentText = str;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //默认RSS地址
            txtPath.Text = "http://feed.cnblogs.com/blog/sitehome/rss";
        }


    }
}

源码下载:RSS阅读器.rar

转载于:https://www.cnblogs.com/zxx193/archive/2013/04/17/3026849.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值