comparison api/Factories/Xml/WarFoundryXmlSaver.cs @ 108:2060f23abee9

Re #53: Create XML saver * Add saving of unit equipment * Add extra property to DTD to indicate whether equipment amount is fixed or a ratio
author IBBoard <dev@ibboard.co.uk>
date Fri, 21 Aug 2009 20:12:27 +0000
parents c4ee96a91018
children 45f1db6356e9
comparison
equal deleted inserted replaced
107:c4ee96a91018 108:2060f23abee9
41 return success; 41 return success;
42 } 42 }
43 43
44 private string CreateXmlString(WarFoundryObject toSave) 44 private string CreateXmlString(WarFoundryObject toSave)
45 { 45 {
46 string xmlString = "";
47
48 if (toSave is Army)
49 {
50 xmlString = CreateArmyXmlString((Army)toSave);
51 }
52
53 return xmlString;
54 }
55
56 private string CreateArmyXmlString(Army toSave)
57 {
46 /* 58 /*
47 <army id="12345" name="Sample Army" system="sampleSystem" race="Empire" maxPoints="500"> 59 <army id="12345" name="Sample Army" system="sampleSystem" race="Empire" maxPoints="500">
48 <units> 60 <units>
49 <unit id="unit1" unitType="Empire1" unitName="General Eustace" size="1"> 61 <unit id="unit1" unitType="Empire1" unitName="General Eustace" size="1">
50 <equipment> 62 <equipment>
63 </equipment> 75 </equipment>
64 </unit> 76 </unit>
65 </units> 77 </units>
66 </army> 78 </army>
67 */ 79 */
68 string xmlString = "";
69
70 if (toSave is Army)
71 {
72 xmlString = CreateArmyXmlString((Army)toSave);
73 }
74
75 return xmlString;
76 }
77
78 private string CreateArmyXmlString(Army toSave)
79 {
80 XmlDocument doc = new XmlDocument(); 80 XmlDocument doc = new XmlDocument();
81 XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", null, null); 81 XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", null, null);
82 doc.AppendChild(declaration); 82 doc.AppendChild(declaration);
83 XmlSchema schema = new XmlSchema(); 83 XmlSchema schema = new XmlSchema();
84 schema.Namespaces.Add("xmlns", "http://ibboard.co.uk/warfoundry/army"); 84 schema.Namespaces.Add("xmlns", "http://ibboard.co.uk/warfoundry/army");
94 XmlElement units = doc.CreateElement("units"); 94 XmlElement units = doc.CreateElement("units");
95 root.AppendChild(units); 95 root.AppendChild(units);
96 96
97 foreach (Unit unit in toSave.GetUnits()) 97 foreach (Unit unit in toSave.GetUnits())
98 { 98 {
99 XmlElement unitElem = doc.CreateElement("unit"); 99 units.AppendChild(CreateUnitElement(unit, doc));
100 unitElem.SetAttribute("id", unit.ID);
101 unitElem.SetAttribute("unitName", unit.Name);
102 unitElem.SetAttribute("unitType", unit.UnitType.ID);
103 unitElem.SetAttribute("size", unit.Size.ToString());
104 100
105 if (!unit.Race.Equals(toSave.Race))
106 {
107 unitElem.SetAttribute("race", unit.Race.ID);
108 }
109
110 units.AppendChild(unitElem);
111 } 101 }
112 102
113 return doc.OuterXml; 103 return doc.OuterXml;
114 } 104 }
105
106 private XmlElement CreateUnitElement(Unit unit, XmlDocument doc)
107 {
108 XmlElement unitElem = doc.CreateElement("unit");
109 unitElem.SetAttribute("id", unit.ID);
110 unitElem.SetAttribute("unitName", unit.Name);
111 unitElem.SetAttribute("unitType", unit.UnitType.ID);
112 unitElem.SetAttribute("size", unit.Size.ToString());
113
114 if (!unit.Race.Equals(unit.Army.Race))
115 {
116 unitElem.SetAttribute("race", unit.Race.ID);
117 }
118
119 XmlElement equipmentElem = CreateEquipmentItemsElement(unit, doc);
120
121 if (equipmentElem != null)
122 {
123 unitElem.AppendChild(equipmentElem);
124 }
125
126 return unitElem;
127 }
128
129 private XmlElement CreateEquipmentItemsElement(Unit unit, XmlDocument doc)
130 {
131 UnitEquipmentItem[] equipItems = unit.GetEquipment();
132 int equipItemCount = equipItems.Length;
133 XmlElement equipmentElem = null;
134
135 if (equipItemCount > 0)
136 {
137 equipmentElem = doc.CreateElement("equipment");
138
139 for (int i = 0; i < equipItemCount; i++)
140 {
141 equipmentElem.AppendChild(CreateEquipmentElement(equipItems[i], unit, doc));
142 }
143 }
144
145 return equipmentElem;
146 }
147
148 private XmlElement CreateEquipmentElement(UnitEquipmentItem item, Unit unit, XmlDocument doc)
149 {
150 XmlElement equipmentItemElem = doc.CreateElement("equipItem");
151 equipmentItemElem.SetAttribute("id", item.EquipmentItemID);
152 equipmentItemElem.SetAttribute("amount", unit.GetEquipmentAmount(item).ToString());
153 equipmentItemElem.SetAttribute("amountType", unit.GetEquipmentAmountIsRatio(item) ? "ratio" : "fixed");
154 return equipmentItemElem;
155 }
115 } 156 }
116 } 157 }