Beispiel #1
0
        /// <summary>
        /// Create a string object with an RSS 2.0 (XML) feed, generated by using the passed params. 
        /// <para>SQL must return fields in the following order:</para>
        /// <para>1. Unique ID for the record, which is appended to the "redirect" param below to create a link to the content.</para>
        /// <para>2. Title for the article.</para>
        /// <para>3. Short description of the article (up to 500 characters, or will be cropped to 500).</para>
        /// <para>4. Date of the article.</para>
        /// </summary>
        /// <example>
        /// <code>
        /// using Argentini.Halide;
        /// ...
        /// string result = H3Xml.BuildRss("SELECT TOP 10 ID, headline, content, date, content FROM News;", "MyDatabase", rssDate, "Feed Title", "http://nonsequiturs.com", "This is the pocketkaos weekly feed.", "(c)opyright 2007", "http://nonsequiturs.com/rss/redirect.aspx?ID=");
        /// </code>
        /// </example>
        /// <param name="sql">SQL SELECT statement for pulling data</param>
        /// <param name="connectionStringName">Connection string name to use for SQL access.</param>
        /// <param name="rssDate">Date of the feed itself</param>
        /// <param name="title">Title of the feed</param>
        /// <param name="link">Link to the source web site (e.g. http://nonsequiturs.com).</param>
        /// <param name="description">Description of the feed (up to 500 characters)</param>
        /// <param name="copyright">Copyright text</param>
        /// <param name="redirect">Path to use for redirection (e.g. "http://nonsequiturs.com/rss/redirect.aspx?ID="). The first column returned will be appended to the end of this string for you.
        /// Passed param is pulled from SQL and appended. This is also used as a unique GUID for the item.</param>
        /// <param name="feedUrl">Fully qualified URL to the generated feed. This is only used to generate an ATOM self-referral tag within the feed itself.</param>
        /// <returns>A string containing the XML for the RSS feed.</returns>
        public static String BuildRss(String sql, String connectionStringName, String rssDate, String title, String link, String description, String copyright, String redirect, String feedUrl)
        {
            System.DateTime thedate = new System.DateTime();
            StringWriter sw = new StringWriter();
            XmlTextWriter xml = new XmlTextWriter(sw);

            thedate = Convert.ToDateTime(rssDate);

            xml.Formatting = Formatting.Indented;
            xml.Indentation = 1;
            xml.IndentChar = (char)9;

            xml.WriteStartDocument();

            #region Write document header

            xml.WriteStartElement("rss");
            xml.WriteAttributeString("version", "2.0");
            xml.WriteAttributeString("xmlns:atom", "http://www.w3.org/2005/Atom");

            #endregion

            xml.WriteStartElement("channel");

            #region Write channel information

            xml.WriteStartElement("title"); xml.WriteString(title); xml.WriteEndElement();
            xml.WriteStartElement("link"); xml.WriteString(link); xml.WriteEndElement();
            xml.WriteStartElement("description"); xml.WriteString(description); xml.WriteEndElement();
            xml.WriteStartElement("language"); xml.WriteString("en-us"); xml.WriteEndElement();
            xml.WriteStartElement("copyright"); xml.WriteString(copyright); xml.WriteEndElement();
            xml.WriteStartElement("pubDate"); xml.WriteString(H3Temporal.DateFormat(thedate, Halide.H3Temporal.DateFormats.Rss2)); xml.WriteEndElement();
            xml.WriteStartElement("lastBuildDate"); xml.WriteString(H3Temporal.DateFormat(DateTime.Now, Halide.H3Temporal.DateFormats.Rss2)); xml.WriteEndElement();
            xml.WriteStartElement("ttl"); xml.WriteString("5"); xml.WriteEndElement();
            xml.WriteStartElement("generator"); xml.WriteString("Halide"); xml.WriteEndElement();

            #endregion

            #region Read in items from database

            Halide.H3Reader reader = new Halide.H3Reader(sql, connectionStringName);

            while (reader.Read())
            {
                xml.WriteStartElement("item");

                xml.WriteStartElement("title"); xml.WriteString(H3Text.StripHtml(H3Text.HtmlRemoveSymbols(reader[1]), true, false)); xml.WriteEndElement();
                xml.WriteStartElement("link"); xml.WriteString(redirect + reader[0]); xml.WriteEndElement();

                string desc = H3Text.StripHtml(reader[2], true, false).Replace("\r", " ").Replace("\n", "");
                String suffix = "";

                if (desc.Length > 400)
                {
                    desc = H3Text.Crop(desc, 400, Halide.H3Text.CropType.Characters, " .,:?!([");
                    suffix = "...";
                }

                while (desc.IndexOf("  ") >= 0)
                {
                    desc = desc.Replace("  ", " ");
                }

                desc = desc.Replace("&#160;", " ");
                desc = desc.Replace("&nbsp;", " ");
                desc = desc.Trim();

                if (desc.LastIndexOf("&") > (desc.Length - 8))
                {
                    desc = desc.Substring(0, desc.LastIndexOf("&"));
                }

                desc = desc.Trim() + suffix;

                xml.WriteStartElement("description"); xml.WriteString(desc); xml.WriteEndElement();
                xml.WriteStartElement("pubDate"); xml.WriteString(H3Temporal.DateFormat(Convert.ToDateTime(reader.GetString(3)), Halide.H3Temporal.DateFormats.Rss2)); xml.WriteEndElement();
                xml.WriteStartElement("guid"); xml.WriteString(redirect + reader[0]); xml.WriteEndElement();

                xml.WriteEndElement();
            }

            reader.Close();

            #endregion

            #region Write out atom-compliant self-link

            xml.WriteStartElement("atom:link");
            xml.WriteAttributeString("href", feedUrl);
            xml.WriteAttributeString("rel", "self");
            xml.WriteAttributeString("type", "application/rss+xml");
            xml.WriteEndElement();

            #endregion

            xml.WriteEndElement();
            xml.WriteEndElement();
            xml.WriteEndDocument();
            xml.Close();

            string[] delim = { "\r\n" };
            string[] decl = sw.ToString().Split(delim, StringSplitOptions.None);
            decl[0] = decl[0].Replace("utf-16", "ISO-8859-1");

            string feed = "";

            for (int x = 0; x < decl.Length; x++)
            {
                feed += decl[x] + "\r\n";
            }

            return (feed);
        }