CodersAdvocate

I work hard, so you don't have to

create an rss feed in csharp

 
namespace RSSFeed
{
    public class Feed
    {
        //<title></title>
        //<link></link>
        //<description></description>
        //<language></language>
        //<pubDate></pubDate>
        //<lastBuildDate></lastBuildDate>
        //<docs></docs>
        //<generator></generator>
        //<managingEditor></managingEditor>
        //<webMaster></webMaster>
        //<ttl></ttl>
        private Tag title;
        public Tag Title {
            get { return title; }
            set {
                title = value;
                title.Element = "title";
            }
        }
        private Tag link;
        public Tag Link {
            get { return link; }
            set {
                link = value;
                link.Element = "link";
            }
        }
        private Tag description;
        public Tag Description {
            get { return description; }
            set {
                description = value;
                description.Element = "description";
            }
        }
        private Tag language;
        public Tag Language {
            get { return language; }
            set {
                language = value;
                language.Element = "language";
            }
        }
        private Tag pubDate;
        public Tag PubDate {
            get { return pubDate; }
            set {
                pubDate = value;
                pubDate.Element = "pubDate";
            }
        }
        private Tag lastBuildDate;
        public Tag LastBuildDate {
            get { return lastBuildDate; }
            set {
                lastBuildDate = value;
                lastBuildDate.Element = "lastBuildDate";
            }
        }
        private Tag docs;
        public Tag Docs {
            get { return docs; }
            set {
                docs = value;
                docs.Element = "docs";
            }
        }
        private Tag generator;
        public Tag Generator {
            get { return generator; }
            set {
                generator = value;
                generator.Element = "generator";
            }
        }
        private Tag managingEditor;
        public Tag ManagingEditor {
            get { return managingEditor; }
            set {
                managingEditor = value;
                managingEditor.Element = "managingEditor";
            }
        }
        private Tag webMaster;
        public Tag WebMaster {
            get { return webMaster; }
            set {
                webMaster = value;
                webMaster.Element = "webMaster";
            }
        }
        private Tag ttl;
        public Tag Ttl {
            get { return ttl; }
            set {
                ttl = value;
                ttl.Element = "ttl";
            }
        }
        private Collection<Item> itemNodes;
        public Collection<Item> ItemNodes {
            get { return itemNodes; }
            set { itemNodes = value; }
        }
        private Image imageNodes;
        public Image ImageNodes {
            get { return imageNodes; }
            set { imageNodes = value; }
        }
        public override string ToString() {
            StringBuilder xmlText = new StringBuilder("");
            xmlText.Append(("<?xml version=\"1.0\"?><rss version=\"2.0\"><channel>"));
            xmlText.Append(title.ToString());
            xmlText.Append(link.ToString());
            xmlText.Append(description.ToString());
            xmlText.Append(language.ToString());
            xmlText.Append(pubDate.ToString());
            xmlText.Append(lastBuildDate.ToString());
            xmlText.Append(docs.ToString());
            xmlText.Append(generator.ToString());
            xmlText.Append(managingEditor.ToString());
            xmlText.Append(webMaster.ToString());
            xmlText.Append(ttl.ToString());
            imageNodes.ToString();
            foreach (Item tag in itemNodes)
            {
                xmlText.Append(tag.ToString());
            }
            xmlText.Append(("</channel></rss>"));
            return xmlText.ToString();
        }
    }
    public class Item
    {
        private Tag title;
        public Tag Title {
            get { return title; }
            set {
                title = value;
                title.Element = "title";
            }
        }
        private Tag link;
        public Tag Link {
            get { return link; }
            set {
                link = value;
                link.Element = "link";
            }
        }
        private Tag description;
        public Tag Description {
            get { return description; }
            set {
                description = value;
                description.Element = "description";
            }
        }
        private Tag pubDate;
        public Tag PubDate {
            get { return pubDate; }
            set {
                pubDate = value;
                pubDate.Element = "pubDate";
            }
        }
        private Tag guid;
        public Tag Guid {
            get { return guid; }
            set
            {
                guid = value;
                guid.Element = "guid";
            }
        }
        public override string ToString() {
            StringBuilder xmlText = new StringBuilder("");
            xmlText.Append(("<item>"));
            xmlText.Append("\t" + title);
            xmlText.Append("\t" + link);
            xmlText.Append("\t" + description);
            xmlText.Append("\t" + pubDate);
            xmlText.Append("\t" + guid);
            xmlText.Append(("</item>\n"));
            return xmlText.ToString();
        }
    }
    public class Image {
        #region Item template
        //XMLBody
        //<item>
        //  <title></title>
        //  <link></link>
        //  <description>description goes here</description>
        //  <pubDate>DateTime.Now("r")</pubDate>
        //  <guid></guid>
        //</item>
        #endregion
        private Tag title;
        public Tag Title {
            get { return title; }
            set {
                title = value;
                title.Element = "title";
            }
        }
        private Tag link;
        public Tag Link {
            get { return link; }
            set {
                link = value;
                link.Element = "link";
            }
        }
        private Tag url;
        public Tag Url {
            get { return url; }
            set {
                url = value;
                url.Element = "url";
            }
        }
        public override string ToString() {
            StringBuilder xmlText = new StringBuilder("");
            xmlText.Append(("<image>"));
            xmlText.Append(title.ToString());
            xmlText.Append(link.ToString());
            xmlText.Append(url.ToString());
            xmlText.Append(("</image>"));
            return xmlText.ToString();
        }
    }
    public class Tag
    {
        private string element;
        public string Element {
            get { return element; }
            set { element = value; }
        }
        private Collection<Attributes> attributes;
        public Collection<Attributes> Attributes {
            get { return attributes; }
            set { attributes = value; }
        }
        private string text;
        public string Text {
            get { return text; }
            set { text = value; }
        }
        private Collection<Tag> childNodes;
        public Collection<Tag> ChildNodes {
            get { return childNodes; }
            set { childNodes = value; }
        }
        public Tag(string _text) {
            text = _text;
            childNodes = new Collection<Tag>();
            attributes = new Collection<Attributes>();
        }
        internal Tag(string _element, Collection<Attributes> _attributes, string _text)
        {
            element = _element;
            attributes = _attributes;
            text = _text;
            childNodes = new Collection<Tag>();
        }
        public override string ToString() {
            StringBuilder xmlText = new StringBuilder("");
            xmlText.Append(string.Format("<{0}{1}>{2}</{0}>\n", element, RenderAttributes(), RenderChildren()));
            return xmlText.ToString();
        }
        private string RenderAttributes() {
            StringBuilder xmlText = new StringBuilder("");
            foreach (Attributes attribute in attributes)
            {
                xmlText.Append(attribute.ToString());
            }
            return xmlText.ToString();
        }
        private string RenderChildren() {
            StringBuilder xmlText = new StringBuilder(HttpUtility.HtmlEncode(HttpUtility.HtmlDecode(StripHTML(text))));
            foreach (Tag childNode in childNodes) {
                xmlText.Append(childNode.ToString());
            }
            return xmlText.ToString();
        }
        private static string StripHTML(string inputString) {
            const string HTML_TAG_PATTERN = "<.*?>";
            return Regex.Replace
              (inputString, HTML_TAG_PATTERN, " ");
        }
    }
    public class Attributes
    {
        private string name;
        public string Name {
            get { return name; }
            set { name = value; }
        }
        private string _value;
        public string Value {
            get { return _value; }
            set { _value = value; }
        }
        public Attributes(string _name, string __value)
        {
            _value = __value;
            name = _name;
        }
        public override string ToString() {
            return string.Format(" {0}=\"{1}\"",name, _value);
        }
    }
}
Copyright 2011 CodersAdvocate ©