public static void GenerateTemplate(string templateXmlFile, string destination, IEnumerable <string> imageNames, Dictionary <string, Dictionary <string, string> > replacementStrings, string title) { var xml = new XmlDocument(); xml.Load(templateXmlFile); var templateNodes = TemplateXmlGenerator.Flatten(xml.ChildNodes).Where( n => n.LocalName == TemplateXmlGenerator.imageName && n.NamespaceURI == TemplateXmlGenerator.NamespaceURI).ToArray(); foreach (var templateNode in templateNodes) { foreach (var imageName in imageNames) { TemplateXmlGenerator.ImplementTemplate(xml, templateNode, imageName, replacementStrings[imageName]); } templateNode.ParentNode.RemoveChild(templateNode); } TemplateXmlGenerator.ImplementTitle(xml, title); var filename = Path.GetFileName(templateXmlFile); if (File.Exists(filename)) { File.Delete(filename); } xml.Save(Path.Combine(destination, filename)); }
private static IEnumerable <XmlNode> Flatten(XmlNodeList nodes) { foreach (XmlNode node in nodes) { yield return(node); foreach (var subNode in TemplateXmlGenerator.Flatten(node.ChildNodes)) { yield return(subNode); } } }
private static void ImplementTitle(XmlDocument xml, string title) { var titleNodes = TemplateXmlGenerator.Flatten(xml.ChildNodes).Where( n => n.LocalName == TemplateXmlGenerator.titleName && n.NamespaceURI == TemplateXmlGenerator.NamespaceURI).ToArray(); foreach (var titleNode in titleNodes) { var newNode = xml.CreateTextNode(title); titleNode.ParentNode.InsertBefore(newNode, titleNode); titleNode.ParentNode.RemoveChild(titleNode); } }
private static void ImplementTemplate(XmlDocument xml, XmlNode templateNode, string imageName, Dictionary <string, string> strings) { var newNode = templateNode.Clone(); var childNodes = new List <XmlNode>(); childNodes.AddRange(TemplateXmlGenerator.Flatten(newNode.ChildNodes)); TemplateXmlGenerator.ReplaceText(strings, childNodes); newNode = xml.ImportNode(newNode, deep: true); childNodes.Clear(); childNodes.AddRange(newNode.ChildNodes.Cast <XmlNode>()); foreach (var childNode in childNodes) { templateNode.ParentNode.InsertBefore(childNode, templateNode); } }
public static void GenerateDetails(string detailXmlFile, string destination, IEnumerable <string> imageNames, Dictionary <string, Dictionary <string, string> > replacementStrings, string title) { var xmlText = File.ReadAllText(detailXmlFile); foreach (var imageName in imageNames) { var xml = new XmlDocument(); xml.LoadXml(xmlText); TemplateXmlGenerator.ReplaceText(replacementStrings[imageName], TemplateXmlGenerator.Flatten(xml.ChildNodes)); TemplateXmlGenerator.ImplementTitle(xml, title); var destinationPath = Path.Combine(destination, imageName + ".xhtml"); if (File.Exists(destinationPath)) { File.Delete(destinationPath); } xml.Save(destinationPath); } }