. Net reads Rss and converts it to DataTable

Source: Internet
Author: User

Recently, you need to use asp.net (c #) to receive an Rss feed. Rss is the data of your forum, and Rss adds a subitem to the Item. Therefore, I wrote a general code to read Rss and immediately add or delete the subitem in Items. This Code is also recognizable.
Hmm ...... Paste the code and record it.
Copy codeThe Code is as follows:
Read Rss and return DataTable
/// <Summary>
/// Get Rss and convert it to DataTable.
/// </Summary>
/// <Param name = "filePath"> Rss address </param>
/// <Returns> </returns>
Public static DataTable GetRss (string filePath)
{
DataTable dt = new DataTable ();
WebClient wc = new WebClient ();
Stream srContent = wc. OpenRead (filePath );

StreamReader sr = new StreamReader (srContent );
If (! Sr. EndOfStream)
{
XmlDocument xmlDoc = new XmlDocument ();
XmlDoc. Load (sr );
XmlNodeList xnl = xmlDoc. SelectNodes ("rss/channel/item ");
If (xnl. Count> 0)
{
{// Add a column ID for the dataTable
XmlNode xnColumn = xnl [0];
XmlNodeList columnsNode = xnColumn. ChildNodes;
Foreach (XmlNode xn in columnsNode)
{
DataColumn dc = new DataColumn (xn. Name );
Dt. Columns. Add (dc );
}
} // The end of adding a column ID for the dataTable

{// Add row data for the DataTable
Foreach (XmlNode xnDate in xnl)
{
DataRow dr = dt. NewRow ();
For (int I = 0; I <dt. Columns. Count; I ++)
{
XmlNode itemValue = xnDate. SelectSingleNode (dt. Columns [I]. ColumnName );
Dr [I] = itemValue. InnerText;
}
Dt. Rows. Add (dr );
}
} // Add column data for the DataTable
}
}
Return dt;
}

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.