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 diff
     1.1 --- a/API/Exporters/WarFoundryXMLWithXSLExporter.cs	Thu Aug 18 11:14:14 2011 -0600
     1.2 +++ b/API/Exporters/WarFoundryXMLWithXSLExporter.cs	Thu Aug 18 11:14:58 2011 -0600
     1.3 @@ -40,8 +40,38 @@
     1.4              // Hide constructor
     1.5          }
     1.6  
     1.7 +        // Write to file
     1.8          public void ExportArmy(Army army, string path)
     1.9          {
    1.10 +            XmlDocument xmlDoc = BuildXml(army);
    1.11 +            // Simple XML output settings
    1.12 +            XmlWriterSettings xmlSettings = new XmlWriterSettings();
    1.13 +            xmlSettings.Indent = true;
    1.14 +            xmlSettings.IndentChars = "  ";
    1.15 +
    1.16 +            // Write XML to file
    1.17 +            using (XmlWriter writer = XmlWriter.Create(path, xmlSettings))
    1.18 +            {
    1.19 +                xmlDoc.Save(writer);
    1.20 +                writer.Flush();
    1.21 +            }
    1.22 +        }
    1.23 +
    1.24 +        // Write to file with transform
    1.25 +        public void ExportArmyWithTransform(Army army, string savePath, string xslPath)
    1.26 +        {
    1.27 +            XmlDocument xmlDoc = BuildXml(army);
    1.28 +            XslCompiledTransform xslTransform = new XslCompiledTransform();
    1.29 +
    1.30 +            xslTransform.Load(xslPath);
    1.31 +            XmlWriter writer = XmlWriter.Create(savePath, xslTransform.OutputSettings);
    1.32 +            xslTransform.Transform(xmlDoc, writer);
    1.33 +            writer.Flush();
    1.34 +        }
    1.35 +        
    1.36 +        // Build the XML document to save or transform
    1.37 +        private XmlDocument BuildXml(Army army)
    1.38 +        {
    1.39              XmlDocument armyList = new XmlDocument();
    1.40             
    1.41              // Everything will be a child of the army element
    1.42 @@ -97,12 +127,7 @@
    1.43                      foreach (UnitEquipmentItem equip in uni.GetEquipment())
    1.44                      {
    1.45                          XmlElement armyEquipmentItem = armyList.CreateElement("equipmentItem");
    1.46 -                        //armyEquipmentItem.SetAttribute("count", equip.CostMultiplier.ToString());
    1.47 -                        
    1.48 -                        // Item Name
    1.49 -                        XmlElement armyEquipmentName = armyList.CreateElement("name");
    1.50 -                        armyEquipmentName.InnerText = equip.Name;
    1.51 -                        armyEquipmentItem.AppendChild(armyEquipmentName);
    1.52 +                        armyEquipmentItem.SetAttribute("name", equip.Name);
    1.53  
    1.54                          int armyEquipAmount = 0;
    1.55  
    1.56 @@ -129,14 +154,9 @@
    1.57                      foreach (Ability abil in uni.Abilities)
    1.58                      {
    1.59                          XmlElement armyAbilityItem = armyList.CreateElement("abilityItem");
    1.60 -                        
    1.61 -                        XmlElement armyAbilityName = armyList.CreateElement("name");
    1.62 -                        armyAbilityName.InnerText = abil.Name;
    1.63 -                        armyAbilityItem.AppendChild(armyAbilityName);
    1.64  
    1.65 -                        XmlElement armyAbilityDesc = armyList.CreateElement("description");
    1.66 -                        armyAbilityDesc.InnerText = abil.Description;
    1.67 -                        armyAbilityItem.AppendChild(armyAbilityDesc);
    1.68 +                        armyAbilityItem.SetAttribute("name", abil.Name);
    1.69 +                        armyAbilityItem.SetAttribute("description", abil.Description);
    1.70  
    1.71                          armyUnit.AppendChild(armyAbilityItem);
    1.72                      }
    1.73 @@ -154,17 +174,7 @@
    1.74              // Append tree to document
    1.75              armyList.AppendChild(root);
    1.76  
    1.77 -            // Simple XML output settings
    1.78 -            XmlWriterSettings xmlSettings = new XmlWriterSettings();
    1.79 -            xmlSettings.Indent = true;
    1.80 -            xmlSettings.IndentChars = "   ";
    1.81 -
    1.82 -            // Write XML to file
    1.83 -            using (XmlWriter writer = XmlWriter.Create(path, xmlSettings))
    1.84 -            {
    1.85 -                armyList.Save(writer);
    1.86 -                writer.Flush();
    1.87 -            }
    1.88 +            return armyList;
    1.89          }
    1.90          private string GetEquipmentAmountRatioTranslation(double amount, int number)
    1.91          {