locked
Need to generate RSS XML file RRS feed

  • Question

  • User-909867351 posted

    Hi 

    I need to generate a XML file with RSS from my webiste. The articles are on a database. I think the general format for RSS is (according http://stackoverflow.com/questions/2839102/how-to-create-rss-feed-for-a-website)

    <?xml version="1.0"?>
    <rss version="2.0">
      <channel>
        <title>Example Channel</title>
        <link>http://example.com/</link>
        <description>My example channel</description>
        <item>
           <title>News for September the Second</title>
           <link>http://example.com/2002/09/01</link>
           <description>other things happened today</description>
        </item>
        <item>
           <title>News for September the First</title>
           <link>http://example.com/2002/09/02</link>
        </item>
      </channel>
    </rss>

    Now my code is not ok. I try this but  not working (it's a mysql database)

     OdbcConnection connection = new OdbcConnection("Dsn=xxxx;server=localhost;uid=xxxx;pwd=xxxx;database=xxxx; port=3306");
            OdbcCommand command = new OdbcCommand("SELECT * from artigos where publicado='1' order by id desc", connection);
            connection.Open();
            OdbcDataReader reader = command.ExecuteReader();
    XDocument doc = new XDocument( new XElement("chanel", new XElement("title", "content"), new XElement("link", "content"), new XElement("description", "content"), while (reader.Read()) { new XElement("item", new XElement("title","titulo 1"), new XElement("link", "titulo 1"), new XElement("description", "titulo 1") ) } ) ); doc.Save("C:\\inetpub\\vhosts\\sorelogios.pt\\httpdocs\\sorelogiosrss.xml"); Response.RedirectPermanent("http://sorelogios.pt/sorelogiosrss.xml");

    The while give me error and how can I write 

    <?xml version="1.0"?>
    <rss version="2.0">

    in my xml file?

    Any help, please?

    Monday, December 19, 2016 12:40 AM

Answers

  • User-707554951 posted

    Hi, mariolopes.

    From your description, I create a RssTable in my database.

    Then I use the following code:

    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Xml;
    
      protected void Page_Load(object sender, EventArgs e)
            {
                XmlDocument xmlDoc = new XmlDocument();
                XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", null, null);
                xmlDoc.AppendChild(dec);
                XmlElement root = xmlDoc.CreateElement("rss");
                root.SetAttribute("Version", "2.0");
                xmlDoc.AppendChild(root);
                XmlNode channel = xmlDoc.CreateElement("Channel");
                root.AppendChild(channel);
    
                string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand("select * from RssTable"))
                    {
                        cmd.Connection = con;
                        con.Open();
                        using (SqlDataReader  redder =cmd.ExecuteReader() )
                        {                    
                            if (redder.HasRows)
                            {
                                while (redder.Read())
                                {
    
                                    XmlNode title = CreateElement(xmlDoc, "title", redder.GetString(0));
                           channel.AppendChild(title);
                                    XmlNode link = CreateElement(xmlDoc, "link", redder.GetString(1));
                                    channel.AppendChild(link);
                                    XmlNode description = CreateElement(xmlDoc, "descritpion", redder.GetString(2));
                                    channel.AppendChild(description);
                                    XmlNode item1Element = xmlDoc.CreateElement("item");
                                   
                                    item1Element.AppendChild(CreateElement(xmlDoc, "title", redder.GetString(3)));
                                    item1Element.AppendChild(CreateElement(xmlDoc, "link", redder.GetString(4)));
                                    item1Element.AppendChild(CreateElement(xmlDoc, "descritpion", redder.GetString(5)));
                                    channel.AppendChild(item1Element);
                                    XmlNode item2Element = xmlDoc.CreateElement("item");
                                
                                    item2Element.AppendChild(CreateElement(xmlDoc, "title", redder.GetString(6)));
                                    item2Element.AppendChild(CreateElement(xmlDoc, "link", redder.GetString(7)));
                                    channel.AppendChild(item2Element);
                                }
                            }
                            xmlDoc.Save(@"C:\Users\v-guzou\Desktop\Test\Rsstest.xml");
                           con.Close();                                              
                        }
                    }
                }
            }
    
           protected XmlNode CreateElement(XmlDocument doc,string name,string text)
            {
                XmlNode nodeElement = doc.CreateElement(name);
                nodeElement.InnerText = text;
                return nodeElement;
            }

    The output screenshot as below:


    Best Regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 20, 2016 5:49 AM