How to RSS your data |
Copy Source | Copy HTML- /// <summary>
- /// Returns XML of the Generic Type.
- /// </summary>
- /// <param name="rssDocument">The RSS document.</param>
- /// <typeparam name="T">RssDocumentBase</typeparam>
- /// <returns>string</returns>
- public static string ToRssXml<T>(T rssDocument) where T : RssDocumentBase
- {
- if (rssDocument == null)
- {
- throw new ArgumentNullException("rssDocument");
- }
-
- MemoryStream memoryStream = new MemoryStream();
- String XmlizedString = null;
- using (XmlTextWriter output = new XmlTextWriter(memoryStream, Encoding.UTF8))
- {
-
- XmlSerializer serializer = new XmlSerializer(typeof(T));
- serializer.Serialize(output, rssDocument);
- memoryStream = (MemoryStream)output.BaseStream;
- XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
- return XmlizedString;
- }
- }
-
- /// <summary>
- /// To convert a Byte Array of Unicode values (UTF-8 encoded) to a complete String.
- /// </summary>
- /// <param name="characters">Unicode Byte Array to be converted to String</param>
- /// <returns>String converted from Unicode Byte Array</returns>
- private static String UTF8ByteArrayToString(Byte[] characters)
- {
- UTF8Encoding encoding = new UTF8Encoding();
- String constructedString = encoding.GetString(characters);
- return (constructedString);
- }
RSS asp- :
Copy Source | Copy HTML- //, XML/RSS
- Response.ContentType = "application/rss+xml";
- //
- using (MySQL db = new MySQL())
- {
- DataSet ds = db.GetData("get_rss");
- if (ds != null && ds.Tables[0].Rows.Count > 0)
- {
- RssDocument rss = new RssDocument()
- {
- Version = "2.0",
- Channel = new RssChannel()
- {
- LastBuildDate = DateTime.Now.ToString(),
- Language = "ru-RU",
- WebMaster = "topbot@ya.ru",
- }
- };
- rss.Channel.Title = "blabla"
- rss.Channel.Link = ds.Tables[0].Rows[0]["linkto"].ToString();
- rss.Channel.Items = new List<RssItem>(0);
- foreach (DataRow dr in ds.Tables[0].Rows)
- {
- RssItem ritem = new RssItem()
- {
- PubDate = ((DateTime)dr["when"]).ToString("s"),
- Description = dr["text"].ToString()
- };
- rss.Channel.Items.Add(ritem);
- }
- Response.Write(rss.ToXml(DocumentType.Rss));
- }
- else
- {
- Response.ContentType = "application/rss+xml";
- }
-
- }