rss.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //
  2. // RSS.cs: Some utility classes to generate RSS feeds
  3. //
  4. // (C) 2002 Miguel de Icaza ([email protected])
  5. //
  6. //
  7. using System;
  8. using System.Xml;
  9. using System.IO;
  10. namespace RSS {
  11. public class Item {
  12. XmlDocument doc;
  13. XmlNode item;
  14. XmlText title, link, description;
  15. public Item (XmlDocument doc, XmlNode item)
  16. {
  17. this.doc = doc;
  18. this.item = item;
  19. }
  20. XmlText MakeTextElement (string name)
  21. {
  22. XmlNode node = doc.CreateElement (name);
  23. XmlText text = doc.CreateTextNode ("");
  24. item.AppendChild (node);
  25. node.AppendChild (text);
  26. return text;
  27. }
  28. public string Title {
  29. get {
  30. if (title == null)
  31. return null;
  32. return title.Value;
  33. }
  34. set {
  35. if (title == null)
  36. title = MakeTextElement ("title");
  37. title.Value = value;
  38. }
  39. }
  40. public string Link {
  41. get {
  42. if (link == null)
  43. return null;
  44. return link.Value;
  45. }
  46. set {
  47. if (link == null)
  48. link = MakeTextElement ("link");
  49. link.Value = value;
  50. }
  51. }
  52. public string Description {
  53. get {
  54. if (description == null)
  55. return null;
  56. return description.Value;
  57. }
  58. set {
  59. if (description == null)
  60. description = MakeTextElement ("description");
  61. description.Value = value;
  62. }
  63. }
  64. }
  65. public class Channel {
  66. XmlDocument doc;
  67. XmlNode channel;
  68. XmlText title, link, description, language, pubDate, lastBuildDate;
  69. XmlText managingEditor, webMaster;
  70. XmlText MakeTextElement (string name)
  71. {
  72. XmlNode node = doc.CreateElement (name);
  73. XmlText text = doc.CreateTextNode ("");
  74. channel.AppendChild (node);
  75. node.AppendChild (text);
  76. return text;
  77. }
  78. public Channel (XmlDocument doc, XmlNode node)
  79. {
  80. this.channel = node;
  81. this.doc = doc;
  82. title = MakeTextElement ("title");
  83. link = MakeTextElement ("link");
  84. description = MakeTextElement ("description");
  85. }
  86. public Item NewItem ()
  87. {
  88. XmlNode node = doc.CreateElement ("item");
  89. Item item;
  90. channel.AppendChild (node);
  91. item = new Item (doc, node);
  92. return item;
  93. }
  94. public string Title {
  95. get {
  96. return title.Value;
  97. }
  98. set {
  99. title.Value = value;
  100. }
  101. }
  102. public string Link {
  103. get {
  104. return link.Value;
  105. }
  106. set {
  107. link.Value = value;
  108. }
  109. }
  110. public string Description {
  111. get {
  112. return description.Value;
  113. }
  114. set {
  115. description.Value = value;
  116. }
  117. }
  118. #region Optional Values
  119. public string ManagingEditor {
  120. get {
  121. if (managingEditor == null)
  122. return null;
  123. return managingEditor.Value;
  124. }
  125. set {
  126. if (managingEditor == null)
  127. managingEditor = MakeTextElement ("managingEditor");
  128. managingEditor.Value = value;
  129. }
  130. }
  131. public string WebMaster {
  132. get {
  133. if (webMaster == null)
  134. return null;
  135. return webMaster.Value;
  136. }
  137. set {
  138. if (webMaster == null)
  139. webMaster = MakeTextElement ("webMaster");
  140. webMaster.Value = value;
  141. }
  142. }
  143. public string PubDate {
  144. get {
  145. if (pubDate == null)
  146. return null;
  147. return pubDate.Value;
  148. }
  149. set {
  150. if (pubDate == null)
  151. pubDate = MakeTextElement ("pubDate");
  152. pubDate.Value = value;
  153. }
  154. }
  155. public string LastBuildDate {
  156. get {
  157. if (lastBuildDate == null)
  158. return null;
  159. return lastBuildDate.Value;
  160. }
  161. set {
  162. if (lastBuildDate == null)
  163. lastBuildDate = MakeTextElement ("lastBuildDate");
  164. lastBuildDate.Value = value;
  165. }
  166. }
  167. public string Language {
  168. get {
  169. if (language == null)
  170. return null;
  171. return language.Value;
  172. }
  173. set {
  174. if (language == null)
  175. language = MakeTextElement ("language");
  176. language.Value = value;
  177. }
  178. }
  179. #endregion
  180. }
  181. class RSS {
  182. XmlDocument doc;
  183. XmlNode rss;
  184. const string rss_base =
  185. "<?xml version=\"1.0\"?> <rss version=\"0.92\"></rss>";
  186. public RSS ()
  187. {
  188. doc = new XmlDocument ();
  189. doc.LoadXml (rss_base);
  190. rss = doc.DocumentElement;
  191. }
  192. public Channel NewChannel (string title, string url)
  193. {
  194. XmlNode node = doc.CreateElement ("channel");
  195. Channel c;
  196. rss.AppendChild (node);
  197. c = new Channel (doc, node);
  198. return c;
  199. }
  200. public XmlDocument XmlDocument {
  201. get {
  202. return doc;
  203. }
  204. }
  205. }
  206. }