| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 785 人关注过本帖
标题:哪位仁兄帮我解决一下,谢谢了!(用List<T>存储频道信息,用Dictionary< ...
取消只看楼主 加入收藏
五岳
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-11-24
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:0 
哪位仁兄帮我解决一下,谢谢了!(用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);
                }
            }
        }
搜索更多相关主题的帖子: Dictionary 加载 List TreeView 控件 
2010-12-13 23:06
快速回复:哪位仁兄帮我解决一下,谢谢了!(用List<T>存储频道信息,用Dictio ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.032002 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved