当前位置:编程技术网 > 入门案例 > 一个简单的支持rss和atom的聚合新闻阅读器 > 文章详细内容

新闻阅读器 几个难点的 代码实现

一、下载RSS的xml内容,并且解析

public void UpdateArticles()

{

    try

    {

        this.Articles.Clear();

        Article art;

        WebClient wc = new WebClient();

        XmlDocument doc = new XmlDocument();

        wc.DownloadFile(this.Url, "temp.xml");

        doc.Load("temp.xml");

        XmlNode docname = doc.DocumentElement;

        switch (docname.Name.ToLower())

        {

            case "rss":

                foreach (XmlNode fxn in docname.FirstChild.ChildNodes)

                {

                    if (fxn.Name == "item")

                    {

                        art = new Article();

                        foreach (XmlNode sxn in fxn.ChildNodes)

                        {

                            switch (sxn.Name)

                            {

                                case "title":

                                    art.Title = sxn.InnerText.Trim();

                                    break;

                                case "link":

                                    art.Url = sxn.InnerText.Trim();

                                    break;

                            }

                        }

                        Articles.Add(art.Title, art);

                    }

                }

                break;

            case "feed":

                foreach (XmlNode sxn in docname.ChildNodes)

                {

                    if (sxn.Name == "entry")

                    {

                        art = new Article();

                        foreach (XmlNode cxn in sxn)

                        {

                            switch (cxn.Name)

                            {

                                case "title":

                                    art.Title = cxn.InnerText.Trim();

                                    break;

                                case "link":

                                    art.Url = cxn.Attributes["href"].Value.ToString().Trim();

                                    break;

                            }

                        }

                        this.Articles.Add(art.Title, art);

                    }

                }

                break;

        }

    }

    catch (Exception)

    {

        System.Windows.Forms.MessageBox.Show("请检查网络连接!");

    }

}

 

 

二、持久化的存储和读取,使用序列化技术

public void Save()

{

    FileStream fs = new FileStream(temp, FileMode.OpenOrCreate, FileAccess.Write);

    BinaryFormatter bf = new BinaryFormatter();

    bf.Serialize(fs, feed);

    fs.Close();

}

 

public void Load()

{

    try

    {

        if (!File.Exists(temp)) return;

        FileStream fs = new FileStream(temp, FileMode.OpenOrCreate, FileAccess.Read);

        BinaryFormatter bf = new BinaryFormatter();

        this.feed = bf.Deserialize(fs) as List<Feed>;

        fs.Close();

    }

    catch

    {

        if (this.feed == null) this.feed = new List<Feed>();

    }

}

 

作者|来源:原创(17fx.net)发表于:2009-2-18 0:00:15