0

我似乎无法在我的 RSS 生成器文件创建的 RSS 提要静态文件的项目中格式化 pubDate。我可以在 RSS 提要的标题中正确生成它,但我无法让 String.Format 正常工作;如果我不使用 {} 括号,如果我使用 {} 包围字符串,它只会继续输出字符串,然后它会导致错误。

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 + ")";
}

}
4

1 回答 1

0

更换

r["pubDate"].ToString()

String.Format("{0:r}", r["pubDate"]) 

解决了这个问题

于 2013-07-06T19:38:20.720 回答