13

您将如何使用 C# 在 ASP.NET 3.5 中创建 RSS 提要?

哪些框架组件可以帮助 .NET 开发人员更轻松地发布 RSS 或 Atom 提要?

.NET 4 中是否有任何额外的功能使这项任务比 3.5 更容易?

4

2 回答 2

14

在 3.5 中有一个新的命名空间,称为System.ServiceModel.Syndication

凯文米勒对此有很好的文章,关于新命名空间的文章并不多,请查看文章,无需在此处重新发布,在我看来示例代码更容易查看。

于 2010-03-10T17:27:40.147 回答
-1
public void BindData()
{                    

    StringWriter sw = new StringWriter();
    XmlTextWriter writer = new XmlTextWriter(sw);
    XmlDocument doc = new XmlDocument();
    XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);

    writer.WriteStartElement("feed");
    writer.WriteAttributeString("xmlns", "http://www.w3.org/2005/Atom");
    writer.WriteString("\n");
    writer.WriteElementString("title", this.TTT + " - " + this.Title);
    writer.WriteString("\n");


    writer.WriteStartElement("link");
    writer.WriteAttributeString("href", this.Url );
    writer.WriteEndElement();

    writer.WriteElementString("id", "urn:uuid:" + Guid.NewGuid().ToString());            
    writer.WriteElementString("updated", DateTime.UtcNow.ToString("o"));


    foreach (var  item in this.lista)
    {
        writer.WriteStartElement("entry");
        writer.WriteElementString("title", item.Value.Title);

        writer.WriteStartElement("link");
        writer.WriteAttributeString("href", item.Key);
        writer.WriteEndElement();

        writer.WriteElementString("id", item.Key);
        string slikaImgUrl = item.Value.Imaga;

        if (string.IsNullOrEmpty(slikaImgUrl) == false)
        {
            writer.WriteStartElement("link");
            writer.WriteAttributeString("rel", "enclosure");
            writer.WriteAttributeString("type", "image/jpeg");
            writer.WriteAttributeString("href", slikaImgUrl);
            writer.WriteEndElement();
        }

        writer.WriteStartElement("author");
        writer.WriteElementString("name", this.Title);
        writer.WriteEndElement();

        writer.WriteStartElement("summary");
        writer.WriteAttributeString("type", "text");
        writer.WriteCData(" ");
        writer.WriteEndElement();               

        writer.WriteElementString("updated", DateTime.UtcNow.ToString("o"));
        writer.WriteElementString("published", DateTime.UtcNow.ToString("o"));
        writer.WriteEndElement();
        writer.WriteString("\n");                
    }

    writer.WriteEndElement();
    string dataOut = sw.ToString();


    Response.Clear();
    Response.ContentType = "text/xml";

    Response.Write(dataOut);
    writer.Close();
    Response.End();
}
于 2015-10-01T10:15:52.233 回答