根据时间表,因此突出点是“快速”并且愿意投入一些钱(不是说我会支付正确的答案,而是购买商品:))。
在.NET framework 2.0中寻找可用的RSS feed生成器。理想情况下,它易于使用(推断快速),但足够灵活,可支持多种标准(shown part way down this page)。
我们有.NET中的业务类,并希望在ASP.NET网站上发布RSS源。
任何好的软件?
不必是免费的
必须支持.NET framework 2.0
感谢。
候选人和推理
答案 0 :(得分:2)
最近我为ASP.NET应用程序创建了一个RSS feed生成器。首先,您来自XmlWriter
。然后,您将创建将托管源的页面。你去了:
public enum RssElements
{
Rss, Channel, Title, Description, Link, Copyright, Generator, Item, PubDate
}
public enum RssAttributes
{
Version
}
/// <summary>
/// RSS writer that emits RSS feeds.
/// </summary>
public class RssWriter : XmlWriter
{
private XmlWriter writer;
private Stream objStream;
#region Constructor
public RssWriter (Stream stream)
{
objStream = stream;
writer = XmlWriter.Create (objStream);
}
#endregion
#region Stream Related Operations
public override void Close ()
{
objStream.Close ();
writer.Close ();
}
public override void Flush ()
{
writer.Flush ();
}
#endregion
#region Writing Elements
public void WriteStartElement (RssElements element)
{
string elementName = "";
switch (element)
{
case RssElements.Channel:
elementName = "channel";
break;
case RssElements.Copyright:
elementName = "copyright";
break;
case RssElements.Description:
elementName = "description";
break;
case RssElements.Generator:
elementName = "generator";
break;
case RssElements.Item:
elementName = "item";
break;
case RssElements.Link:
elementName = "link";
break;
case RssElements.PubDate:
elementName = "pubDate";
break;
case RssElements.Rss:
elementName = "rss";
break;
case RssElements.Title:
elementName = "title";
break;
}
writer.WriteStartElement (elementName);
}
public void WriteElementString (RssElements element, string value)
{
string elementName = "";
switch (element)
{
case RssElements.Channel:
elementName = "channel";
break;
case RssElements.Copyright:
elementName = "copyright";
break;
case RssElements.Description:
elementName = "description";
break;
case RssElements.Generator:
elementName = "generator";
break;
case RssElements.Item:
elementName = "item";
break;
case RssElements.Link:
elementName = "link";
break;
case RssElements.PubDate:
elementName = "pubDate";
break;
case RssElements.Rss:
elementName = "rss";
break;
case RssElements.Title:
elementName = "title";
break;
}
writer.WriteElementString (elementName, value);
}
public override void WriteEndElement () { writer.WriteEndElement (); }
#endregion
#region Writing Attributes
public void WriteStartAttribute (RssAttributes attb)
{
if (attb == RssAttributes.Version)
{
writer.WriteStartAttribute ("version");
}
}
public void WriteAttributeString (RssAttributes attb, string value)
{
if (attb == RssAttributes.Version)
{
writer.WriteAttributeString ("version", value);
}
}
public override void WriteEndAttribute () { writer.WriteEndAttribute (); }
#endregion
#region Writing Data
public override void WriteCData (string text) { writer.WriteCData (text); }
public override void WriteChars (char [] buffer, int index, int count) { writer.WriteChars (buffer, index, count); }
public override void WriteComment (string text) { writer.WriteComment (text); }
public override void WriteWhitespace (string ws) { writer.WriteWhitespace (ws); }
public override void WriteString (string text) { writer.WriteString (text); }
#endregion
#region Document
public override void WriteStartDocument () { writer.WriteStartDocument (); }
public override void WriteStartDocument (bool standalone) { writer.WriteStartDocument (standalone); }
public override void WriteEndDocument () { writer.WriteEndDocument (); }
#endregion
#region Not Implemented Methods
void ThrowException () { throw new Exception ("The method or operation is not implemented."); }
public override string LookupPrefix (string ns) { ThrowException (); return ""; }
public override void WriteBase64 (byte [] buffer, int index, int count) { ThrowException (); }
public override void WriteCharEntity (char ch) { ThrowException (); }
public override void WriteStartElement (string prefix, string localName, string ns) { ThrowException (); }
public override WriteState WriteState
{
get { throw new Exception ("The method or operation is not implemented."); }
}
public override void WriteSurrogateCharEntity (char lowChar, char highChar) { ThrowException (); }
public override void WriteEntityRef (string name) { ThrowException (); }
public override void WriteFullEndElement () { ThrowException (); }
public override void WriteProcessingInstruction (string name, string text) { ThrowException (); }
public override void WriteRaw (string data) { ThrowException (); }
public override void WriteRaw (char [] buffer, int index, int count) { ThrowException (); }
public override void WriteStartAttribute (string prefix, string localName, string ns) { ThrowException (); }
public override void WriteDocType (string name, string pubid, string sysid, string subset) { ThrowException (); }
#endregion
}
然后创建主页。这是一个空白的页面;所有工作都在Page_Load事件中完成。下面我给你一个Page_Load事件。注意:您可以使用业务对象替换数据库对象。
protected void Page_Load (object sender, EventArgs e)
{
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "text/xml";
RssWriter writer = new RssWriter (Response.OutputStream);
writer.WriteStartElement (RssElements.Rss);
writer.WriteAttributeString (RssAttributes.Version, "2.0");
writer.WriteStartElement (RssElements.Channel);
writer.WriteElementString (RssElements.Title, "XYZ corp.com");
writer.WriteElementString (RssElements.Link, "http://www.yourdomain.com");
writer.WriteElementString (RssElements.Description, "Latest news from XYZ corp");
writer.WriteElementString (RssElements.Copyright, "Copyright (C) XYZ corp. All rights reserved.");
writer.WriteElementString (RssElements.Generator, "XYZ corp XML RSS Generator");
DataTable newsTable = NewsAccess.GetLatestNewsByDate (false);
if (newsTable.Rows.Count > 0) {
for (int i = 0; i < newsTable.Rows.Count; ++i)
{
string id = newsTable.Rows [i] ["LatestNewsID"].ToString ();
string subject = newsTable.Rows [i] ["Subject"].ToString ();
string description = newsTable.Rows [i] ["Description"].ToString ();
bool publicView = newsTable.Rows [i] ["PublicView"].ToString () == "True" ? true : false;
if (!publicView)
continue;
//string details = newsTable.Rows [i] ["Details"].ToString ();
DateTime dt = DateTime.Parse (newsTable.Rows [i] ["DateCreated"].ToString ());
string date = dt.ToString ("R");
writer.WriteStartElement (RssElements.Item);
writer.WriteElementString (RssElements.Title, subject);
writer.WriteElementString (RssElements.Link, "http://www.yourdomain.com/Contents/News/newsarticle.aspx?id=" + id.ToString ());
writer.WriteElementString (RssElements.Description, description);
writer.WriteElementString (RssElements.PubDate, date);
writer.WriteEndElement ();
}
}
writer.WriteEndElement ();
writer.WriteEndElement ();
writer.Close ();
Response.End ();
}
我希望它有所帮助。
答案 1 :(得分:1)
这里是我用来从BLL中的集合生成rss feed的一些vb.net代码:
Dim xws As New XmlWriterSettings()
xws.Encoding = Encoding.UTF8
Using xw As XmlWriter = XmlTextWriter.Create(XmlFileName, xws)
xw.WriteStartDocument()
xw.WriteStartElement("rss")
xw.WriteAttributeString("version", "2.0")
xw.WriteStartElement("channel")
xw.WriteElementString("title", Title)
xw.WriteElementString("link", Link)
xw.WriteElementString("description", Description)
xw.WriteElementString("managingEditor", Editor)
Dim items As System.Collections.Generic.List(Of FolderDocuments) = FolderDocuments.GetFolderDocuments()
If items IsNot Nothing Then
For Each fd As FolderDocuments In items
xw.WriteStartElement("item")
xw.WriteElementString("title", fd.Title)
xw.WriteElementString("description", fd.Intro)
xw.WriteElementString("link", fd.Url)
xw.WriteElementString("pubDate", fd.DateStart.ToString("R"))
xw.WriteEndElement()
Next
Else
xw.WriteStartElement("item")
xw.WriteEndElement()
End If
xw.WriteEndElement()
xw.WriteEndElement()
xw.WriteEndDocument()
End Using
这会写入文件,但您可以轻松地使用通用处理程序(.ashx)将参数传递给动态生成所需rss的参数 - 使用针对xml writer的Response.Output。