RSS production (C #)-Using XML DOM

Source: Internet
Author: User
Dom|rss|xml

RSS for Web sites is generally referenced in two ways. One is an existing XML file that is then updated when the database is updated, or it is updated with other maintenance programs. The other is the dynamic generation of RSS files, that is, when access to an address, the server method from the database to read the latest records, generate RSS files, returned to visitors.

Now describes the method of dynamically generating RSS files.

There are basically two ways to generate RSS files dynamically, one is to add strings to the other, and the other is to use XML document generation methods. String accumulation method is also relatively simple, I will not say more, here emphasize the generation of XmlDocument methods, including the creation of various nodes, the creation of attributes. Of course here it is also necessary to explain why the latter, because the latter conforms to the XML DOM standard, to help you understand the DOM model, and build faster, the structure of the XML document is more error-prone, some of the details I will do some of the necessary to tell.

The main methods are as follows:

private void Writerss ()

{

XmlDocument domdoc = new XmlDocument ();

XmlDeclaration nodedeclar = domdoc.createxmldeclaration ("1.0", System.Text.Encoding.UTF8.BodyName, "yes");

Domdoc.appendchild (Nodedeclar);

If the RSS has a style sheet file, add these two sentences

XmlProcessingInstruction Nodestylesheet = domdoc.createprocessinginstruction ("Xml-stylesheet", "type=\" Text/css\ " Href=\ "Rss.css\");

Domdoc.appendchild (Nodestylesheet);

XmlElement root = domdoc.createelement ("RSS");

Root.  SetAttribute ("Version", "2.0"); To add a property node

Domdoc.appendchild (root);

XmlElement chnode = domdoc.createelement ("channel");

Root. AppendChild (Chnode);

XmlElement element = domdoc.createelement ("title");

XmlNode Textnode = Domdoc.createtextnode ("Sohu Focus News"); Text node

Element. AppendChild (Textnode);

Chnode. AppendChild (Element);

element = domdoc.createelement ("link");

Textnode = Domdoc.createtextnode ("http://www.sohu.com");

Element. AppendChild (Textnode);

Chnode. AppendChild (Element);

element = domdoc.createelement ("description"); Reference node

XmlNode Cdatanode = domdoc.createcdatasection ("Instant report on current affairs and global Focus events");

Element. AppendChild (Cdatanode);

Chnode. AppendChild (Element);

DataTable dt = Getdatatab (); Access the database to get the records to be displayed in RSS

foreach (DataRow dr in Dt. Rows)

{

element = Domdoc.createelement ("item");

//...

Create content nodes, common such as title,description,link,pubdate, creating methods ditto

//...

Chnode. AppendChild (Element);

}

Output

XmlTextWriter objtextwrite = new XmlTextWriter (this. RESPONSE.OUTPUTSTREAM,SYSTEM.TEXT.ENCODING.UTF8);

Domdoc.writeto (Objtextwrite);

Objtextwrite.flush ();

Objtextwrite.close ();

}

The output is as follows (the item section is added manually for the description instance):

<?xml version= "1.0" encoding= "Utf-8"?>

<rss version= "2.0" >

<channel>

<title> Sohu Focus News </title>

<link>http://www.sohu.com</link>

<description>

<! [cdata[to report on the events of global focus in China

]]>

</description>

<item id= "" >

<title></title>

<link></link>

<pubdate>2006-10-15 21:59:36</pubdate>

</item>

<item id= "" >

<title></title>

<link></link>

<pubdate>2006-10-15 10:33:53</pubdate>

</item>

<title>[intermediary [sale of residential] Ming FA International New Town 3 Room 2 Hall 2 Wei 930,000 yuan/set </title>

<link>http://www.ewhouse.com/HouseInfo.aspx?publishId=3440</link>

<pubdate>2006-10-12 10:50:18</pubdate>

</item>

</channel>

</rss>

There are a few points to note:

1, createTextNode, that is, create a text node

Some people used to use innertext to add text in the node, although the result is the same, but to know that in the DOM is also a node in Chinese, since to meet the DOM standard, it is going to the end!

2, output

I use XmlTextWriter output in the instance.

You can actually use the following:

Response.ContentType = "Application/xml"; Output and displayed as XML data

Response.Write (Domdoc.innerxml);

However, using XmlTextWriter output is faster, so this method is also recommended.



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.