[ASP.NET] 使用 XmlTextWriter 發佈 RSS Publish

[ASP.NET] 使用 XmlTextWriter 發佈 RSS Publish

主要就是要配合 rss 所規定好的欄位,再把資料庫的資料,對應到相關的欄位即可。

   if (dsData.Tables[0].Rows.Count == 1)
            {

                context.Response.Clear();
                context.Response.ContentType = "text/xml";
                System.Xml.XmlTextWriter xmlWrite = new System.Xml.XmlTextWriter(context.Response.OutputStream, Encoding.UTF8);
                xmlWrite.WriteStartDocument();
                xmlWrite.WriteStartElement("rss");
                xmlWrite.WriteAttributeString("version", "2.0");
                xmlWrite.WriteStartElement("channel");
                xmlWrite.WriteElementString("title","¤¤°ê¤å¤Æ¤j¾Ç±À¼s±Ð¨|³¡ " + dsData.Tables[0].Rows[0]["Block_Name_1000"].ToString());
                xmlWrite.WriteElementString("link", "http://www.sce.pccu.edu.tw");
                xmlWrite.WriteElementString("description", dsData.Tables[0].Rows[0]["Comment_1000"].ToString());
                xmlWrite.WriteElementString("copyright", "");
                xmlWrite.WriteElementString("ttl", "15");


                foreach (DataRow var in dsData.Tables[1].Rows)
                {
                    
                    xmlWrite.WriteStartElement("item");
                    xmlWrite.WriteElementString("title", var["title"].ToString());
                    xmlWrite.WriteElementString("ttl", "30");
                    xmlWrite.WriteElementString("pubDate", Convert.ToDateTime(var["PubDate"].ToString()).AddHours(-8).ToString("R"));
                    xmlWrite.WriteElementString("description", var["Description"].ToString());
                    xmlWrite.WriteElementString("link", var["Link"].ToString());
 

                    xmlWrite.WriteEndElement();

                }


                xmlWrite.WriteEndElement();
                xmlWrite.WriteEndElement();
                xmlWrite.WriteEndDocument();
                xmlWrite.Flush();
                xmlWrite.Close();
                context.Response.End();
            }