哪位仁兄帮我解决一下,谢谢了!(用List<T>存储频道信息,用Dictionary<T,V>存储文章信息,怎么样才能把它们加载到TreeView控件中
/// <summary>/// 读取配置信息
/// </summary>
public void Load()
{
string displayName; //频道标题
string url; //频道地址
string description; //频道描述
int feedCount; //频道个数
FileStream fs = new FileStream("user.profile", FileMode.Open);
StreamReader sr = new StreamReader(fs);
feedCount = Convert.ToInt32(sr.ReadLine());
profile.Feeds.Clear();
//利用频道个数循环,并将频道对象保存到泛型集合中
for (int i = 0; i < feedCount; i++)
{
displayName = sr.ReadLine();
url = sr.ReadLine();
description = sr.ReadLine();
profile.Feeds.Add(new RssFeed_1(displayName, url, description, 0));
}
sr.Close();
fs.Close();
}
///<summary>
///user.profile文件
///</summary>
2
红楼梦
中国古典名著
localhost/MyNewsReader/HongLouMeng.txt
西游记
中国古典名著
localhost/MyNewsReader/西游记.txt
/// <summary>
/// 将每个频道下的文章标题保存到Dictionary<string, Article>中
/// </summary>
/// <returns></returns>
public bool FetchArticles()
{
string Url = "http://localhost/MyNewsReader/Feeds.Xml";
string filepath = "Feeds.Xml";
try
{
//如果文章列表为空
if (this.articles == null)
{
this.articles = new Dictionary<string, Article>();
Articles.Clear();
WebClient myClient = new WebClient();
myClient.DownloadFile(Url, filepath);
XmlDocument myXml = new XmlDocument();
myXml.Load(filepath);
XmlNode channel = myXml.DocumentElement.FirstChild;
foreach (XmlNode node in channel.ChildNodes)
{
if (node.Name == "item")
{
Article article = new Article(); //Article指Article类
//内层循环
foreach (XmlNode subNode in node.ChildNodes)
{
switch (subNode.Name)
{
case "title":
article.Title = subNode.InnerText;
break;
case "link":
article.Url = subNode.InnerText;
break;
}
}
//将文章添加到文章列表Dictionary<string, Article>集合中
Articles.Add(article.Title, article);
}
}
}
Clicks++;
return true;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return false;
}
}
///<summary>
///Feeds.Xml文件
///</summary>
<rss version= "2.0">
<channel>
<title>红楼梦</title>
<description>中国古典名著</description>
<link>http://localhost/MyNewsReader/HongLouMeng.txt</link>
<item>
<title>红楼梦第1回</title>
<description>甄士隐梦幻识通灵 贾雨村风尘怀闺秀</description>
<link>http://localhost/MyNewsReader/HongLouMeng/1.TXT</link>
</item>
<item>
<title>红楼梦第2回</title>
<description>贾夫人仙逝扬州城 冷子兴演说荣国府</description>
<link>http://localhost/MyNewsReader/HongLouMeng/2.TXT</link>
</item>
<item>
<title>红楼梦第3回</title>
<description>贾雨村夤缘复旧职 林黛玉抛父进京都</description>
<link>http://localhost/MyNewsReader/HongLouMeng/3.TXT</link>
</item>
</channel>
<channel>
<title>西游记</title>
<description>中国古典名著</description>
<link>http://localhost/MyNewsReader/西游记.txt</link>
<item>
<title>西游记</title>
<description>九九归真</description>
<link>http://localhost/MyNewsReader/西游记/1149.txt</link>
</item>
</channel>
</rss>
/// <summary>
/// 读取配置信息(获取频道信息并加载此频道下的所有文章标题)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnLoadProfile_Click(object sender, EventArgs e)
{
//调用Load()方法
pm.Load();
//如果TreeView控件中已加载所需要加载的根节点,则返回,不做任何处理
if (tvMain.Nodes.Count > 0)
{
return;
}
//遍历频道集合(二重循环)
foreach (RssFeed_1 feed in pm.profile.Feeds)
{
TreeNode feedNode;
feedNode = tvMain.Nodes.Add(feed.DisplayName);
foreach (Article articles in feed.Articles.Values)
{
feedNode.Nodes.Add(articles.Title);
}
}
}