//频道的基类using System;
using System.Collections.Generic;
using System.Text;namespace mynewsReader
{[Serializable]
public abstract class FeedBase
{
public FeedBase() { }
public FeedBase(string displayName,string url,string description,int clicks)
{
this.DisplayName = displayName;
this.Url = url;
this.Description = description;
this.Clicks = clicks;
//事例化存储文章的集合
this.Articles = new Dictionary<string, Article>();
}
//存储文章列表的泛型集合
private Dictionary<string, Article> articles; public Dictionary<string, Article> Articles
{
get { return articles; }
set { articles = value; }
}
//频道标题
private string displayName; public string DisplayName
{
get { return displayName; }
set { displayName = value; }
}
//频道url;
private string url; public string Url
{
get { return url; }
set { url = value; }
}
//频道描述
private string description; public string Description
{
get { return description; }
set { description = value; }
}
//点击次数
private int clicks; public int Clicks
{
get { return clicks; }
set { clicks = value; }
} public abstract bool FetchArticles();
}
}//两个子类using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Net;
using System.Data.SqlClient;
using System.Windows.Forms;namespace mynewsReader
{
[Serializable]
class AtomFeed:FeedBase
{
public AtomFeed() { }
public AtomFeed(string title,string url):base(title ,url,"",0) { }
public AtomFeed(string title,string url,string description):base(title ,url ,description ,0) { }
public AtomFeed(string title, string url, string description, int clicks) : base(title, url, description, 0) { }
//获取文章列表的方法
public override bool FetchArticles()
{
string filePath = "temp.atom";//文件路径
try
{
if (this.Articles == null)
this.Articles = new Dictionary<string, Article>(); this.Articles.Clear();
//取得Atom格式文件
WebClient myclient = new WebClient();
myclient.DownloadFile(Url,filePath);
//读取xml文档
XmlDocument myxml = new XmlDocument();
myxml.Load(filePath);
//获取根节点
XmlNode feedNode = myxml.DocumentElement;
//外层按文章节点循环
foreach (XmlNode node in feedNode.ChildNodes)
{
if(node.Name =="entry")
{
Article atc = new Article();//实例化文章对象
//内层循环获取文章的详细信息
foreach (XmlNode subNode in node.ChildNodes)
{
switch (subNode .Name )
{
case "title":
atc.Title = subNode.InnerText;
break;
case "link":
atc.Url = subNode.Attributes["href"].Value.ToString();
break;
}
}
//将文章信息存入泛型集合中
this.Articles.Add(atc .Title ,atc );
}
}
Clicks++;
return true;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message );
return false;
}
}
}
}
using System.Collections.Generic;
using System.Text;namespace mynewsReader
{[Serializable]
public abstract class FeedBase
{
public FeedBase() { }
public FeedBase(string displayName,string url,string description,int clicks)
{
this.DisplayName = displayName;
this.Url = url;
this.Description = description;
this.Clicks = clicks;
//事例化存储文章的集合
this.Articles = new Dictionary<string, Article>();
}
//存储文章列表的泛型集合
private Dictionary<string, Article> articles; public Dictionary<string, Article> Articles
{
get { return articles; }
set { articles = value; }
}
//频道标题
private string displayName; public string DisplayName
{
get { return displayName; }
set { displayName = value; }
}
//频道url;
private string url; public string Url
{
get { return url; }
set { url = value; }
}
//频道描述
private string description; public string Description
{
get { return description; }
set { description = value; }
}
//点击次数
private int clicks; public int Clicks
{
get { return clicks; }
set { clicks = value; }
} public abstract bool FetchArticles();
}
}//两个子类using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Net;
using System.Data.SqlClient;
using System.Windows.Forms;namespace mynewsReader
{
[Serializable]
class AtomFeed:FeedBase
{
public AtomFeed() { }
public AtomFeed(string title,string url):base(title ,url,"",0) { }
public AtomFeed(string title,string url,string description):base(title ,url ,description ,0) { }
public AtomFeed(string title, string url, string description, int clicks) : base(title, url, description, 0) { }
//获取文章列表的方法
public override bool FetchArticles()
{
string filePath = "temp.atom";//文件路径
try
{
if (this.Articles == null)
this.Articles = new Dictionary<string, Article>(); this.Articles.Clear();
//取得Atom格式文件
WebClient myclient = new WebClient();
myclient.DownloadFile(Url,filePath);
//读取xml文档
XmlDocument myxml = new XmlDocument();
myxml.Load(filePath);
//获取根节点
XmlNode feedNode = myxml.DocumentElement;
//外层按文章节点循环
foreach (XmlNode node in feedNode.ChildNodes)
{
if(node.Name =="entry")
{
Article atc = new Article();//实例化文章对象
//内层循环获取文章的详细信息
foreach (XmlNode subNode in node.ChildNodes)
{
switch (subNode .Name )
{
case "title":
atc.Title = subNode.InnerText;
break;
case "link":
atc.Url = subNode.Attributes["href"].Value.ToString();
break;
}
}
//将文章信息存入泛型集合中
this.Articles.Add(atc .Title ,atc );
}
}
Clicks++;
return true;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message );
return false;
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Xml;namespace mynewsReader
{
[Serializable]
class RssFeed:FeedBase
{
public RssFeed() { }
public RssFeed(string title,string url):base(title ,url,"",0) { }
public RssFeed(string title,string url,string description):base(title ,url ,description ,0) { }
public RssFeed(string title,string url,string description,int clicks):base(title ,url ,description ,0) { } public override bool FetchArticles()
{
string filePath = "temp.rss";
try
{
if (this.Articles == null)
this.Articles = new Dictionary<string, Article>();
Articles.Clear();
WebClient webclient = new WebClient();
webclient.DownloadFile(Url, filePath);
XmlDocument myxml = new XmlDocument();
myxml.Load(filePath);
//获取父节点下的第一个字节点,定位channel节点
XmlNode channel = myxml.DocumentElement.FirstChild;
//定位item节点,外层循环channel
foreach (XmlNode node in channel.ChildNodes)
{
if (node.Name == "item")
{
Article atcl = new Article();
//内层循环item
foreach (XmlNode subnode in node.ChildNodes)
{
if (subnode.Name == "title")
{
atcl.Title = subnode.InnerText;
}
if (subnode.Name == "link")
{
atcl.Url = subnode.InnerText;
}
}
//将文章添加到文章列表
Articles.Add(atcl.Title, atcl);
}
}
return true;
}
catch (Exception ex)
{ Console.WriteLine(ex.ToString ());
return false;
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms; namespace mynewsReader
{
public partial class MyNewsReader : Form
{
public MyNewsReader()
{
InitializeComponent();
} private void MyNewsReader_Load(object sender, EventArgs e)
{
ProfileManager pm = new ProfileManager();
//pm.profile.intialFeeds();
// pm.save();
// pm.Load();
pm.ChannelReset();
//遍历频道集合
foreach (FeedBase feed in pm .profile .Feeds)
{
TreeNode feednode;
//将一个频道加入节点
feednode = treeView1.Nodes.Add(feed .DisplayName);
feednode.Tag = feed;
}
} private void treeview_afterSelect(object sender, TreeViewEventArgs e)
{
TreeNode n = this.treeView1.SelectedNode;
if (n.Level == 1)
{
FeedBase f1 = (FeedBase)n.Tag;
this.webBrowser1.Url = new Uri("http://rss.sina.com.cn/news/marquee/ddt.xml");
} if (n.Level != 0) return;
FeedBase f = (FeedBase)n.Tag; f.FetchArticles();
foreach (string title in f.Articles.Keys)
{
TreeNode nod; nod = n.Nodes.Add(title);
nod.BeginEdit();
//this.treeView1.SelectedNode.Nodes.Add(title,f.Articles[title].ToString());
} }
}
}红颜色的这段代码,点击哪个节点,即点哪个频道,相应地把该频道的所有文章标题作为他的子节点展示出来
但是就是出不来,文章集合里面也有数据,怎么添加不上子节点,我感觉这段代码好像很正确,看不出错在哪?希望高收指点