Use Visual C # To create a news reader

Source: Internet
Author: User
I. Clarify the structure and try again

To easily extract RSS information, you must first understand its structure. The so-called "know yourself, and know what you want.

1. RSS Structure

First, we open an RSS link for Baidu news. If you open another RSS link for several other websites, you will find that they all have the same structure. What we have explained to you in revealing the story of RSS (I) is actually compiling an XML file to implement it.

To facilitate the processing of such XML documents, we use C # As the development language in this article.

After analyzing the entire RSS link, we know that the structure of RSS is shown in Figure 1.


2. Extraction Principle

Knowing the structure, we also need to know the meaning of each part of the structure. In Figure 1, the RSS node indicates that the current RSS file is composed of a channel node and Its subnodes. Some subnodes provide information about the channel itself, for example, title indicates the channel name ("Baidu Internet News ").

The channel node contains multiple item subnodes, and the item node is the part to be processed by the program, because it corresponds to each actual news item information, each item node provides detailed information about the news through its subnodes. For example, the title indicates the news title ("Microsoft IM is King"), and the link corresponds to the actual news link.

RSS specifications view http://blogs.law.harvard.edu/tech/rss

After knowing this, programming is not difficult. We only need to extract and display the information under the channel and item. Now let's take a look at the specific implementation methods.

  2. Make a program to read news

After having some knowledge of RSS, let's start programming. First, we need a simple interface. Create a new win form project, place a label on the form, a text box txturl is used to enter the RSS link (the address contained in the RSS link of each website), and a button bnread is used to read news, A Treeview tree control treerss displays the news items read.

1. Define the load structure

Based on the RSS structure analyzed above, we first create an RSS class to load each item of the channel and item in the RSS link. The Code is as follows:

Public class RSS
{
Public struct Channel
{
Public String title;
Public hashtable items;
}

Public struct item
{
Public String title;
Public String description;
Public String link;
}
}

The channel structure stores information about all the subnodes contained in the channel node. The items member field is a hashtable set, and the program adds the item structure as an object to the set, stores all item nodes in a channel. Here I only read a limited number of nodes. Readers can extend the entire structure definition as needed.

 

2. Obtain news from RSS links

Now we can write the READ function and put the extracted RSS information into the structure designed above.

C # provides special classes to access XML, so that we can easily read RSS content. The Code is as follows:

Xmltextreader reader = new xmltextreader (URL );
Xmlvalidatingreader valid = new xmlvalidatingreader (Reader );
Valid. validationtype = validationtype. None;
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (Reader );

After using the xmldocument class to load the RSS link input in txturl, first find the channel node through the foundchildnode function.

Private xmlnode foundchildnode (xmlnode node, string name)
{
Xmlnode childlnode = NULL;
For (INT I = 0; I <node. childnodes. Count; I ++)
{
If (node. childnodes [I]. Name = Name & node. childnodes [I]. childnodes. Count> 0)
{
Childlnode = node. childnodes [I];
Return childlnode;
}
}
Return childlnode;
}
Xmlnode rssnode = foundchildnode (xmldoc, "RSS ");
Xmlnode channelnode = foundchildnode (rssnode, "channel ");

Then we can traverse its subnodes and read the information we need based on the name attribute of the subnode.

RSS. Channel channel = New RSS. Channel ();
Channel. Items = new hashtable ();
{
Switch (channelnode. childnodes [I]. Name)
{
Case "title ":
{
Channel. Title = channelnode. childnodes [I]. innertext;
Break;
}
Case "item ":
{
RSS. item = This. getrssitem (channelnode. childnodes [I]);
Channel. Items. Add (Channel. Items. Count, item );
Break;
}
}
}

If it is found that it is an item subnode, call the getrssitem function. Similarly, by traversing the subnode, enter the content of the subnode in the item structure and add it to the items set of the channel structure. Because this program does not care about the key value added to the set, it only needs to be a non-repeated value, so I passed in the count attribute.

3. display the read information in the program

After reading the RSS content, you need to display the information to the user. Here we use the basic Treeview method to traverse the items set of the channel structure and add its title to the Treeview.

Private void viewrss (RSS. Channel channel)
{
Treerss. beginupdate ();
Treerss. nodes. Clear ();
Treenode channelnode = treerss. nodes. Add (Channel. Title );
Channelnode. Tag = "";
For (INT I = 0; I <channel. Items. Count; I ++)
{
RSS. item = (RSS. Item) channel. items [I];
Treenode itemnode = channelnode. nodes. Add (item. Title );
Itemnode. Tag = item. Link;
}
Treerss. expandall ();
Treerss. endupdate ();
}

You can also set the tag attribute of each subnode of the Treeview as its link. In this way, you can access specific information by reading tag attributes when selecting subnodes.

Private void treerss_afterselect (Object sender, system. Windows. Forms. treevieweventargs E)
{
Treenode itemnode = E. node;
String url = itemnode. Tag. tostring ();
If (URL. length! = 0)
System. Diagnostics. process. Start (URL );
}

Program Running Effect 2.

 

  Iii. Summary

How about it? A simple RSS news reader can be easily completed as mentioned above. Although it still has many shortcomings, it is enough if you have learned the basic method of extracting RSS link information through this example!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.