# HG changeset patch # User Dan.Kulinski@dank-laptop.Global.Local # Date 1313687698 21600 # Node ID 5d7584a73d7f01ea5195110f220b41c4f580728d # Parent cf8fc32e020a001f7aa25ba3e9ab031a53299649 XSL with transform operations diff -r cf8fc32e020a -r 5d7584a73d7f API/Exporters/WarFoundryXMLWithXSLExporter.cs --- a/API/Exporters/WarFoundryXMLWithXSLExporter.cs Thu Aug 18 11:14:14 2011 -0600 +++ b/API/Exporters/WarFoundryXMLWithXSLExporter.cs Thu Aug 18 11:14:58 2011 -0600 @@ -40,8 +40,38 @@ // Hide constructor } + // Write to file public void ExportArmy(Army army, string path) { + XmlDocument xmlDoc = BuildXml(army); + // Simple XML output settings + XmlWriterSettings xmlSettings = new XmlWriterSettings(); + xmlSettings.Indent = true; + xmlSettings.IndentChars = " "; + + // Write XML to file + using (XmlWriter writer = XmlWriter.Create(path, xmlSettings)) + { + xmlDoc.Save(writer); + writer.Flush(); + } + } + + // Write to file with transform + public void ExportArmyWithTransform(Army army, string savePath, string xslPath) + { + XmlDocument xmlDoc = BuildXml(army); + XslCompiledTransform xslTransform = new XslCompiledTransform(); + + xslTransform.Load(xslPath); + XmlWriter writer = XmlWriter.Create(savePath, xslTransform.OutputSettings); + xslTransform.Transform(xmlDoc, writer); + writer.Flush(); + } + + // Build the XML document to save or transform + private XmlDocument BuildXml(Army army) + { XmlDocument armyList = new XmlDocument(); // Everything will be a child of the army element @@ -97,12 +127,7 @@ foreach (UnitEquipmentItem equip in uni.GetEquipment()) { XmlElement armyEquipmentItem = armyList.CreateElement("equipmentItem"); - //armyEquipmentItem.SetAttribute("count", equip.CostMultiplier.ToString()); - - // Item Name - XmlElement armyEquipmentName = armyList.CreateElement("name"); - armyEquipmentName.InnerText = equip.Name; - armyEquipmentItem.AppendChild(armyEquipmentName); + armyEquipmentItem.SetAttribute("name", equip.Name); int armyEquipAmount = 0; @@ -129,14 +154,9 @@ foreach (Ability abil in uni.Abilities) { XmlElement armyAbilityItem = armyList.CreateElement("abilityItem"); - - XmlElement armyAbilityName = armyList.CreateElement("name"); - armyAbilityName.InnerText = abil.Name; - armyAbilityItem.AppendChild(armyAbilityName); - XmlElement armyAbilityDesc = armyList.CreateElement("description"); - armyAbilityDesc.InnerText = abil.Description; - armyAbilityItem.AppendChild(armyAbilityDesc); + armyAbilityItem.SetAttribute("name", abil.Name); + armyAbilityItem.SetAttribute("description", abil.Description); armyUnit.AppendChild(armyAbilityItem); } @@ -154,17 +174,7 @@ // Append tree to document armyList.AppendChild(root); - // Simple XML output settings - XmlWriterSettings xmlSettings = new XmlWriterSettings(); - xmlSettings.Indent = true; - xmlSettings.IndentChars = " "; - - // Write XML to file - using (XmlWriter writer = XmlWriter.Create(path, xmlSettings)) - { - armyList.Save(writer); - writer.Flush(); - } + return armyList; } private string GetEquipmentAmountRatioTranslation(double amount, int number) {