0

我在下面有一个 xml 文件

<?xml version="1.0" encoding="utf-8"?><rss version="2.0">
 <channel>
  <title>About RSS</title>
  <link>http://localhost:27549/TTTT.aspx</link>
  <description>The latest news</description>
  <image><url>http://localhost:27549/images/ttt_logo.jpg</url></image>
  <item>
    <title>ABC</title>
    <link>http://localhost:27549/Viewttt.aspx?id=217</link>
    <description>zzzzzzzzzzzzzzzzzzz...</description>
    <pubDate>Tuesday, August 30, 2011, 00:00:00AM</pubDate>
  </item>
 </channel>
</rss>

虽然您可以看到 pubdate 标签,但它不会显示在 pubDate 位置。这是我获取 pubdate 的代码,但它不起作用,

DateTime dt = DateTime.ParseExact(pubDate, "MM/dd/yyyy", CultureInfo.InvariantCulture);
pubDate = dt.ToString("dddd, MMMM dd, yyyy, HH:mm:sstt");


writer.WriteElementString("pubDate", pubDate);

例如,我尝试获取今天的日期,如下所示,

writer.WriteElementString("pubDate", DateTime.Now.ToString("r"));

并且日期正在显示。第一组代码可能有什么问题?

String pubDate = "";

            using (System.Data.Common.DbCommand dbCommand = DataAccess.Instance().Database.GetStoredProcCommand("usp_GetLatestNews"))
            {

                using (IDataReader reader = DataAccess.Instance().Database.ExecuteReader(dbCommand))
                {
                    int i = 0;
                    while (reader.Read())
                    {
                        if (i == 0)
                        {
                            newsHeader = "New News Summary Available for " + reader["Title"].ToString() + " - " + reader["PubDate"];
                            newsLink = "ViewTTT.aspx?id=" + reader["Id"].ToString();
                            newsDesc = reader["FullDescription"].ToString();
                            pubDate = reader["pubDate"].ToString();
                            DateTime dt = DateTime.ParseExact(pubDate, "MM/dd/yyyy", CultureInfo.InvariantCulture);

                            pubDate = dt.ToString("r");
                        }
                        i++;
                    }

                };

            }

AddRSSItem(writer, newsHeader, newsUrl, newsDesc, pubDate);
.............
.............

public XmlTextWriter AddRSSItem(XmlTextWriter writer,
                 string sItemTitle, string sItemLink,
                 string sItemDescription, String pubDate)
        {
            writer.WriteStartElement("item");
            writer.WriteElementString("title", sItemTitle);
            writer.WriteElementString("link", sItemLink);
            writer.WriteElementString("description", sItemDescription);
            writer.WriteElementString("pubDate", pubDate);
            writer.WriteEndElement();

            return writer;
        }
4

1 回答 1

1

我的建议是你使用你所说的格式:

DateTime dt = DateTime.ParseExact(pubDate, "MM/dd/yyyy", CultureInfo.InvariantCulture);
pubDate = dt.ToString("r");
writer.WriteElementString("pubDate", pubDate);

请注意,我使用的是"r"指定符Tue, 30 Aug 2011 00:00:00 GMT而不是"dddd, MMMM dd, yyyy, HH:mm:sstt" 2011 年 8 月 30 日星期二 00:00:00 AM,它们是不同的格式。

于 2012-06-08T16:06:13.090 回答