I can't seem to format the pubDate in the items of the RSS feed static file created by my RSS generator file. I can generate it properly in the header of the RSS feed, but I can not get the String.Format to work properly; it just keeps outputting the string if I do not use the {} brackets if I use the {} to surround the string, then it causes an error.
public void WriteToFile()
{
string strDate = null;
strDate = "ddd, dd MMM yyyy HH:mm:ss";
File.Delete(Server.MapPath(".") + "\\" + "rss.xml");
StreamWriter StrWer = default(StreamWriter);
try
{
StrWer = new StreamWriter(Server.MapPath(".") + "\\" + "rss.xml", true);
StrWer.WriteLine("<rss version='2.0' xmlns:content = 'http://purl.org/rss/1.0/modules/content/'>");
StrWer.WriteLine(" <channel>");
StrWer.WriteLine(" <title>" + Master.Banner() + " RSS Feed</title>");
StrWer.WriteLine(" <link>" + Master.SiteSearch() + "</link>");
StrWer.WriteLine(" <description>This contains the news of the " + Master.H1first() + " </description>");
StrWer.WriteLine(" <language>en-us</language>");
StrWer.WriteLine(" <image>");
StrWer.WriteLine(" <url>" + Master.SiteSearch() + "images/site/paclogo.png</url>");
StrWer.WriteLine(" <title>" + Master.Banner() + " Logo</title>");
StrWer.WriteLine(" <link>" + Master.SiteSearch() + "</link>");
StrWer.WriteLine(" </image>");
DateTime time = DateTime.Now; // Use current time
StrWer.WriteLine(" <lastBuildDate>" + time.ToString(strDate) + " EST</lastBuildDate>");
SqlCeConnection conn = null;
conn = new SqlCeConnection(ConfigurationManager.ConnectionStrings["XMLDataSourceReplacement"].ToString());
string sql = "select * from rsssource order by pubDate desc";
SqlCeCommand cmdGetOldMasterId = new SqlCeCommand(sql, conn);
SqlCeDataAdapter da = new SqlCeDataAdapter(cmdGetOldMasterId);
DataTable dt = new DataTable();
da.Fill(dt);
foreach(DataRow r in dt.Rows)
{
StrWer.WriteLine(" <item>");
StrWer.WriteLine(" <title>" + r["title"].ToString() + "</title>");
StrWer.WriteLine(" <link>" + r["link"].ToString() + "</link>");
StrWer.WriteLine(" <description>" + r["description"].ToString() + "</description>");
//"ddd, dd MMM yyyy HH:mm:ss"
string strdate = r["pubDate"].ToString();
string strFormatted = String.Format("{ddd, dd MMM yyyy HH:mm:ss zzz}", strdate);
//string strFormatted = String.Format("ddd, dd MMM yyyy HH:mm:ss zzz", strdate);
StrWer.WriteLine(" test: " + strFormatted);
StrWer.WriteLine(" <pubDate>" + r["pubDate"].ToString() + "</pubDate>");
StrWer.WriteLine(" </item>");
}
StrWer.WriteLine(" </channel>");
StrWer.WriteLine("</rss>");
StrWer.Close();
this.lblStatus.Text = "The rss.xml file update succeeded.";
}
catch (Exception ex)
{
this.lblStatus.Text = "The rss.xml file update failed. (" + ex.Message + ")";
}
}