我现在有一个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);
对于exmaple,我尝试了今天的日期,如下所示,
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;
}
答案 0 :(得分:1)
我的建议是你使用你所声明的格式:
DateTime dt = DateTime.ParseExact(pubDate, "MM/dd/yyyy", CultureInfo.InvariantCulture);
pubDate = dt.ToString("r");
writer.WriteElementString("pubDate", pubDate);
观察我正在使用"r"
说明符星期二,2011年8月30日00:00:00 GMT ,而不是"dddd, MMMM dd, yyyy, HH:mm:sstt"
星期二,8月30日, 2011年,00:00:00 AM ,这是不同的格式。