public static string GenerateAsString(PodcastFeed feed) { using (var stream = new MemoryStream()) { Generate(stream, feed); stream.Flush(); return(Encoding.UTF8.GetString(stream.ToArray())); } }
public static void Generate(Stream stream, PodcastFeed feed) { // not my job to close your stream for you var writer = new XmlTextWriter(stream, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false)); writer.WriteStartDocument(); writer.WriteStartElement("rss"); writer.WriteAttributeString("xmlns", "itunes", null, "http://www.itunes.com/dtds/podcast-1.0.dtd"); writer.WriteAttributeString("version", "2.0"); writer.WriteStartElement("channel"); writer.WriteElementString("title", feed.Title); writer.WriteElementString("description", feed.Description); writer.WriteElementString("lastBuildDate", DateTime.UtcNow.ToString("r")); writer.WriteElementString("pubDate", DateTime.UtcNow.ToString("r")); foreach (var ep in feed.Episodes) { writer.WriteStartElement("item"); writer.WriteElementString("title", ep.Title); writer.WriteElementString("link", ep.DownloadUrl); writer.WriteElementString("guid", ep.DownloadUrl); writer.WriteElementString("description", ep.Description); writer.WriteElementString("pubDate", ep.AddedAt.ToString("r")); writer.WriteStartElement("enclosure"); writer.WriteAttributeString("url", ep.DownloadUrl); writer.WriteEndElement(); //</enclosure> writer.WriteEndElement(); //</item> } writer.WriteEndElement(); // </channel> writer.WriteEndElement(); // </rss> writer.WriteEndDocument(); writer.Flush(); }