Created
February 10, 2019 06:38
-
-
Save SajjadArifGul/7b4f0849f2a54b83d5cc67e7b6a1540f to your computer and use it in GitHub Desktop.
Creates a simple RSS feed for blogs/articles etc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Creates a simple RSS feed for blogs/articles etc | |
/// </summary> | |
/// <remarks></remarks> | |
public class RSSFeed | |
{ | |
/// <summary> | |
///A custom string writer which outputs in UTF8 rather than UTF16 | |
///</summary> | |
///<remarks></remarks> | |
internal class UTF8StringWriter : StringWriter | |
{ | |
public override Encoding Encoding | |
{ | |
get | |
{ | |
return System.Text.Encoding.UTF8; | |
} | |
} | |
public UTF8StringWriter(StringBuilder stringBuilder) : base(stringBuilder) | |
{ | |
} | |
public UTF8StringWriter(StringBuilder stringBuilder, System.IFormatProvider format) : base(stringBuilder, format) | |
{ | |
} | |
} | |
private XmlDocument _document; | |
/// <summary> | |
///Returns the XML document | |
///</summary> | |
///<value></value> | |
///<returns></returns> | |
///<remarks></remarks> | |
private XmlDocument Document | |
{ | |
get | |
{ | |
return _document; | |
} | |
} | |
/// <summary> | |
///Returns a UTF8 string representation of the XML document | |
///</summary> | |
///<value></value> | |
///<returns></returns> | |
///<remarks></remarks> | |
public new string ToString | |
{ | |
get | |
{ | |
var b = new StringBuilder(); | |
using (var w = new UTF8StringWriter(b)) | |
{ | |
_document.Save(w); | |
} | |
return b.ToString(); | |
} | |
} | |
/// <summary> | |
///Initialise the XML document and create the opening rss tags | |
///</summary> | |
///<remarks></remarks> | |
public RSSFeed() | |
{ | |
_document = new XmlDocument(); | |
Document.AppendChild(Document.CreateNode(XmlNodeType.XmlDeclaration, null, null)); | |
// Create the root element and add any name spaces | |
var rootelement = Document.CreateElement("rss"); | |
rootelement.SetAttribute("version", "2.0"); | |
Document.AppendChild(rootelement); | |
// Add the name spaces we use | |
AddNamespace("dc", "http://purl.org/dc/elements/1.1/"); | |
AddNamespace("content", "http://purl.org/rss/1.0/modules/content/"); | |
} | |
/// <summary> | |
///Adds a prefix and url as a namespace to the rss root element | |
///</summary> | |
///<param name="prefix">The prefix for the namespace</param> | |
///<param name="url">The reference url</param> | |
///<remarks></remarks> | |
public void AddNamespace(string prefix, string url) | |
{ | |
Document.DocumentElement.SetAttribute("xmlns:" + prefix, url); | |
} | |
/// <summary> | |
///Creates the channel in the RSS feed | |
///</summary> | |
///<param name="title">The title</param> | |
///<param name="link">The link to the full website</param> | |
///<param name="description">A brief description</param> | |
///<param name="dateLastChanged">The date/time the channel was last updated</param> | |
///<param name="language">The language of the channel</param> | |
///<remarks></remarks> | |
public void CreateChannel(string title, string link, string description, string language) | |
{ | |
// First check we haven't already created a chnanel, as there should only be one in the feed | |
var channels = Document.GetElementsByTagName("channel"); | |
if (channels.Count > 0) | |
throw new ArgumentException("Channel " + channels[0].Name + " has already been created"); | |
var mainchannel = channels[0]; | |
// Create the channel element | |
mainchannel = Document.CreateElement("channel"); | |
mainchannel.AppendChild(CreateTextElement("title", title)); | |
mainchannel.AppendChild(CreateTextElement("link", link)); | |
mainchannel.AppendChild(CreateTextElement("description", description)); | |
mainchannel.AppendChild(CreateTextElement("language", language)); | |
Document.DocumentElement.AppendChild(mainchannel); | |
} | |
/// <summary> | |
///Writes an item to the channel | |
///</summary> | |
///<param name="Title">The title of the item</param> | |
///<param name="Link">A link to the full post</param> | |
///<param name="Description">A description of the items</param> | |
///<remarks></remarks> | |
public void WriteRSSItem(string Title, string Link, string Description) | |
{ | |
// First check we haven't already created a chnanel, as there should only be one in the feed | |
var channels = Document.GetElementsByTagName("channel"); | |
if (channels.Count == 0) | |
throw new ArgumentException("Please create a channel first by calling CreateChannel"); | |
var mainchannel = channels[0]; | |
// Create an item | |
var thisitem = Document.CreateElement("item"); | |
thisitem.AppendChild(CreateTextElement("title", Title)); | |
thisitem.AppendChild(CreateTextElement("link", Link)); | |
thisitem.AppendChild(CreateTextElement("description", Description)); | |
// Append the element | |
mainchannel.AppendChild(thisitem); | |
} | |
/// <summary> | |
///Creates a simple text element with the given name and inner text value | |
///</summary> | |
///<param name="Name">The name of the element</param> | |
///<param name="Value">The value of it</param> | |
///<returns></returns> | |
///<remarks></remarks> | |
private XmlElement CreateTextElement(string Name, string Value) | |
{ | |
XmlElement CreateTextElement = Document.CreateElement(Name); | |
CreateTextElement.InnerText = Value; | |
return CreateTextElement; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment