public static void SerializeXml(FeedType feedType, IFeed feed, Stream output, string xsltUrl, bool prettyPrint) { if (feedType == null) { throw new ArgumentNullException(nameof(feedType)); } // Do we need to transform the feed? if (feedType != feed.FeedType) { feed = feedType.GetFeed(feed); } // Setup document formatting, make human readable XmlWriterSettings settings = new XmlWriterSettings { CheckCharacters = true, CloseOutput = true, ConformanceLevel = ConformanceLevel.Document, Encoding = System.Text.Encoding.UTF8 }; if (prettyPrint) { settings.Indent = true; settings.IndentChars = "\t"; } else { settings.Indent = false; settings.NewLineChars = string.Empty; } settings.NewLineHandling = NewLineHandling.Replace; XmlWriter writer = XmlWriter.Create(output, settings); // Add a stylesheet for browser viewing if (!string.IsNullOrEmpty(xsltUrl)) { // Render the XSLT processor instruction writer.WriteProcessingInstruction("xml-stylesheet", $"type=\"text/xsl\" href=\"{xsltUrl}\" version=\"1.0\""); } // Get all namespaces XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces(); feed.AddNamespaces(namespaces); // Serialize feed XmlSerializer serializer = new XmlSerializer(feed.GetType()); serializer.Serialize(writer, feed, namespaces); }
public static void SerializeXml(FeedType feedType, IFeed feed, Stream output, string xsltUrl) => SerializeXml(feedType, feed, output, xsltUrl, true);
public static void SerializeXml(FeedType feedType, IFeed feed, Stream output) => SerializeXml(feedType, feed, output, null);
private IDocument GenerateFeed(FeedType feedType, Feed feed, ContextConfig path, IExecutionContext context) { // Get the output path FilePath outputPath = path?.Invoke<FilePath>(context); if (outputPath == null) { return null; } if (!outputPath.IsRelative) { throw new ArgumentException("The feed output path must be relative"); } // Generate the feed and document MemoryStream stream = new MemoryStream(); FeedSerializer.SerializeXml(feedType, feed, stream); stream.Position = 0; return context.GetDocument(stream, new MetadataItems { new MetadataItem(Keys.RelativeFilePath, outputPath), new MetadataItem(Keys.WritePath, outputPath) }); }