Atom10Serializer.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // System.ServiceModel.Syndication.Atom10Serializer
  3. //
  4. // Authors:
  5. // Joel Reed ([email protected])
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System;
  28. using System.Globalization;
  29. using System.Xml;
  30. namespace System.ServiceModel.Syndication
  31. {
  32. public class Atom10Serializer : SyndicationSerializer
  33. {
  34. internal override void WriteXml(XmlWriter writer, SyndicationFeed feed)
  35. {
  36. writer.WriteStartElement(FeedName, FeedNamespace);
  37. if (!String.IsNullOrEmpty(feed.Language))
  38. writer.WriteAttributeString("xml:lang", feed.Language);
  39. WriteXml(writer, feed.Title, "title", true);
  40. writer.WriteElementString("id", feed.Id);
  41. WriteXml(writer, feed.Copyright, "rights");
  42. string updated = feed.LastUpdatedTime.ToUniversalTime().ToString("s");
  43. writer.WriteElementString("updated", updated + "Z");
  44. if (feed.ImageUrl != null)
  45. writer.WriteElementString("logo", feed.ImageUrl.ToString());
  46. if (!String.IsNullOrEmpty(feed.Generator))
  47. writer.WriteElementString("generator", feed.Generator);
  48. WriteXml(writer, feed.Description, "summary");
  49. foreach (SyndicationItem item in feed.Items)
  50. {
  51. WriteTo(writer, item);
  52. }
  53. writer.WriteEndElement();
  54. }
  55. internal override void WriteXml(XmlWriter writer, SyndicationItem item)
  56. {
  57. writer.WriteStartElement(ItemName, ItemNamespace);
  58. writer.WriteElementString("id", item.Id);
  59. WriteXml(writer, item.Title, "title");
  60. WriteXml(writer, item.Summary, "summary");
  61. DateTime defDate = new DateTime(0);
  62. if (item.PublishDate != defDate)
  63. {
  64. string date = item.PublishDate.ToUniversalTime().ToString("s");
  65. writer.WriteElementString("published", date + "Z");
  66. }
  67. if (item.LastUpdatedTime != defDate)
  68. {
  69. string date = item.LastUpdatedTime.ToUniversalTime().ToString("s");
  70. writer.WriteElementString("updated", date + "Z");
  71. }
  72. WriteXml(writer, item.Content, "content");
  73. WriteXml(writer, item.Copyright, "rights");
  74. writer.WriteEndElement();
  75. }
  76. protected override string FeedName {
  77. get { return "feed"; }
  78. }
  79. protected override string FeedNamespace {
  80. get { return "http://www.w3.org/2005/Atom"; }
  81. }
  82. protected override string ItemName {
  83. get { return "entry"; }
  84. }
  85. protected override string ItemNamespace {
  86. get { return "http://www.w3.org/2005/Atom"; }
  87. }
  88. }
  89. }