PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/JTacticalSim.DataContext/Conversion/XMLConversion.cs

https://github.com/Queztionmark/JTacticalSim
C# | 183 lines | 139 code | 35 blank | 9 comment | 2 complexity | 2d0c0fb8310039de065edc0d64d0fae6 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using JTacticalSim.API.DTO;
  6. using System.Xml.Linq;
  7. namespace JTacticalSim.DataContext
  8. {
  9. public static class XMLConversion
  10. {
  11. public static XElement ToXML(this INodeDTO dto)
  12. {
  13. XElement x = new XElement("Node");
  14. XElement l = new XElement("Location");
  15. l.Add(dto.Location.ToXML());
  16. x.Add(l);
  17. XElement c = new XElement("Country");
  18. c.Add(new XAttribute("ID", dto.Country));
  19. x.Add(c);
  20. XElement dt = new XElement("DefaultTile");
  21. dt.Add(dto.DefaultTile.ToXML());
  22. x.Add(dt);
  23. return x;
  24. }
  25. public static XElement ToXML(this IUnitDTO dto)
  26. {
  27. XElement x = new XElement("Unit");
  28. x.Add(new XAttribute("ID", dto.ID));
  29. x.Add(new XAttribute("Name", dto.Name));
  30. x.Add(new XAttribute("Description", dto.Description));
  31. x.Add(new XAttribute("StackOrder", dto.StackOrder));
  32. XElement l = new XElement("Location");
  33. // Null location indicates an unplaced unit
  34. if (dto.Location != null)
  35. {
  36. l.Add(dto.Location.ToXML());
  37. }
  38. XElement snl = new XElement("SubNodeLocation");
  39. snl.Add(new XAttribute("Value", dto.SubNodeLocation));
  40. l.Add(snl);
  41. x.Add(l);
  42. XElement ui = new XElement("UnitInfo");
  43. ui.Add(new XAttribute("UnitType", dto.UnitType));
  44. ui.Add(new XAttribute("UnitClass", dto.UnitClass));
  45. ui.Add(new XAttribute("UnitGroupType", dto.UnitGroupType));
  46. x.Add(ui);
  47. XElement ms = new XElement("MovementStats");
  48. ms.Add(new XAttribute("CurrentMovementPoints", dto.CurrentMovementPoints));
  49. ms.Add(new XAttribute("CurrentHasPerformedAction", dto.CurrentHasPerformedAction));
  50. ms.Add(new XAttribute("CurrentRemoteFirePoints", dto.CurrentRemoteFirePoints));
  51. x.Add(ms);
  52. XElement c = new XElement("Country");
  53. c.Add(new XAttribute("ID", dto.Country));
  54. x.Add(c);
  55. //XElement tus = new XElement("TransportedUnits");
  56. //dto.UnitsTransported.ForEach(ut =>
  57. // {
  58. // XElement unit = new XElement("TransportedUnit");
  59. // unit.Add(new XAttribute("ID", ut));
  60. // tus.Add(unit);
  61. // });
  62. //x.Add(tus);
  63. return x;
  64. }
  65. public static XElement ToXML(this ICoordinateDTO dto)
  66. {
  67. XElement x = new XElement("Coordinate");
  68. x.Add(new XAttribute("X", dto.X));
  69. x.Add(new XAttribute("Y", dto.Y));
  70. x.Add(new XAttribute("Z", dto.Z));
  71. return x;
  72. }
  73. public static XElement ToXML(this ICountryDTO dto)
  74. {
  75. XElement x = new XElement("Country");
  76. x.Add(new XAttribute("ID", dto.ID));
  77. x.Add(new XAttribute("Name", dto.Name));
  78. x.Add(new XAttribute("Description", dto.Description));
  79. x.Add(new XAttribute("Faction", dto.Faction));
  80. return x;
  81. }
  82. public static XElement ToXML(this IFactionDTO dto)
  83. {
  84. XElement x = new XElement("Faction");
  85. x.Add(new XAttribute("ID", dto.ID));
  86. x.Add(new XAttribute("Name", dto.Name));
  87. x.Add(new XAttribute("Description", dto.Description));
  88. return x;
  89. }
  90. public static XElement ToXML(this IPlayerDTO dto)
  91. {
  92. XElement x = new XElement("Player");
  93. x.Add(new XAttribute("ID", dto.ID));
  94. x.Add(new XAttribute("Name", dto.Name));
  95. x.Add(new XAttribute("Description", dto.Description));
  96. x.Add(new XAttribute("Country", dto.Country));
  97. x.Add(new XAttribute("ReinforcementPoints", dto.ReinforcementPoints ?? 0));
  98. x.Add(new XAttribute("IsCurrentPlayer", dto.IsCurrentPlayer));
  99. x.Add(new XAttribute("IsAIPlayer", dto.IsAIPlayer));
  100. XElement unrfcmts = new XElement("UnplacedReinforcements");
  101. foreach (var i in dto.UnplacedReinforcements)
  102. {
  103. XElement unit = new XElement("Unit");
  104. unit.Add(new XAttribute("ID", i));
  105. unrfcmts.Add(unit);
  106. }
  107. x.Add(unrfcmts);
  108. return x;
  109. }
  110. public static XElement ToXML(this ITileDTO dto)
  111. {
  112. XElement x = new XElement("Tile");
  113. x.Add(new XAttribute("VictoryPoints", dto.VictoryPoints));
  114. XElement ds = new XElement("Demographics");
  115. dto.Demographics.ToList().ForEach(d =>
  116. {
  117. ds.Add(d.ToXML());
  118. });
  119. x.Add(ds);
  120. return x;
  121. }
  122. public static XElement ToXML(this IDemographicDTO dto)
  123. {
  124. XElement x = new XElement("Demographic");
  125. x.Add(new XAttribute("ID", dto.ID));
  126. return x;
  127. }
  128. public static XElement ToXML(this IScenarioDTO dto)
  129. {
  130. XElement x = new XElement("Scenario");
  131. x.Add(new XAttribute("ID", dto.ID));
  132. x.Add(new XAttribute("Name", dto.Name));
  133. x.Add(new XAttribute("GameFileDirectory", dto.GameFileDirectory));
  134. x.Add(new XAttribute("Author", dto.Author));
  135. return x;
  136. }
  137. public static XElement ToXML(this ISavedGameDTO dto)
  138. {
  139. XElement x = new XElement("SavedGame");
  140. x.Add(new XAttribute("ID", dto.ID));
  141. x.Add(new XAttribute("Name", dto.Name));
  142. x.Add(new XAttribute("GameFileDirectory", dto.GameFileDirectory));
  143. x.Add(new XAttribute("LastPlayed", dto.LastPlayed));
  144. x.Add(new XAttribute("Scenario", dto.Scenario));
  145. return x;
  146. }
  147. }
  148. }