Mercurial > repos > IBBoard.WarFoundry.API
changeset 404:5d7584a73d7f xsl-output
XSL with transform operations
author | Dan.Kulinski@dank-laptop.Global.Local |
---|---|
date | Thu, 18 Aug 2011 11:14:58 -0600 |
parents | cf8fc32e020a |
children | 557afa796848 |
files | API/Exporters/WarFoundryXMLWithXSLExporter.cs |
diffstat | 1 files changed, 34 insertions(+), 24 deletions(-) [+] |
line wrap: on
line diff
--- 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) {