通过解析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