RSS application (2) RSS Extraction

Source: Internet
Author: User

The RSS format conforms to the RSS standard. To extract RSS information, you must first understand the RSS standard format.

Basic RSS format

We open a RSS link http://zqs.cnblogs.com/Rss.aspx and we can see that RSS complies with the following format

In the figureRSSThe node indicates that the current RSS file is composedChannelNodes and Their subnodes. Some subnodes provide information about the channel itself. For example, the title indicates the channel name ("Baidu Internet News ").

ChannelThe node contains multipleItemSub-nodes, andItemThe node isProgramThe part to be processed, because it corresponds to each actual news item information, eachItemThe Node also provides detailed information about the news through its subnodes. For example, the title indicates the news title ("Microsoft Im king") and the link corresponds to the actual news link.

We only need to extract and displayChannelAndItemThe following information is enough.

Extraction Method

Based on the RSS structure analyzed above, we first create an RSS class, it is used to load the channel and item items In the RSS link. Code :
public class RSS
{public struct channel
{Public String title;
Public hashtable items;
}

public struct item
{Public String title;
Public String description;
Public String link;
}< BR >}< br> the channel structure stores information about all subnodes contained in the channel node. The items member field is a hashtable set, the program adds the item structure as an object to the set to store all item nodes in the channel. Here I only read a limited number of nodes. Readers can extend the entire structure definition as needed.

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.

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 );}

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.