/JTacticalSim.DataContext/Conversion/XMLConversion.cs
C# | 183 lines | 139 code | 35 blank | 9 comment | 2 complexity | 2d0c0fb8310039de065edc0d64d0fae6 MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using JTacticalSim.API.DTO;
- using System.Xml.Linq;
-
- namespace JTacticalSim.DataContext
- {
- public static class XMLConversion
- {
- public static XElement ToXML(this INodeDTO dto)
- {
- XElement x = new XElement("Node");
- XElement l = new XElement("Location");
- l.Add(dto.Location.ToXML());
- x.Add(l);
-
- XElement c = new XElement("Country");
- c.Add(new XAttribute("ID", dto.Country));
- x.Add(c);
-
- XElement dt = new XElement("DefaultTile");
- dt.Add(dto.DefaultTile.ToXML());
- x.Add(dt);
-
- return x;
- }
-
- public static XElement ToXML(this IUnitDTO dto)
- {
- XElement x = new XElement("Unit");
- x.Add(new XAttribute("ID", dto.ID));
- x.Add(new XAttribute("Name", dto.Name));
- x.Add(new XAttribute("Description", dto.Description));
- x.Add(new XAttribute("StackOrder", dto.StackOrder));
-
- XElement l = new XElement("Location");
-
- // Null location indicates an unplaced unit
- if (dto.Location != null)
- {
- l.Add(dto.Location.ToXML());
- }
-
- XElement snl = new XElement("SubNodeLocation");
- snl.Add(new XAttribute("Value", dto.SubNodeLocation));
- l.Add(snl);
- x.Add(l);
-
-
- XElement ui = new XElement("UnitInfo");
- ui.Add(new XAttribute("UnitType", dto.UnitType));
- ui.Add(new XAttribute("UnitClass", dto.UnitClass));
- ui.Add(new XAttribute("UnitGroupType", dto.UnitGroupType));
- x.Add(ui);
-
- XElement ms = new XElement("MovementStats");
- ms.Add(new XAttribute("CurrentMovementPoints", dto.CurrentMovementPoints));
- ms.Add(new XAttribute("CurrentHasPerformedAction", dto.CurrentHasPerformedAction));
- ms.Add(new XAttribute("CurrentRemoteFirePoints", dto.CurrentRemoteFirePoints));
- x.Add(ms);
-
- XElement c = new XElement("Country");
- c.Add(new XAttribute("ID", dto.Country));
- x.Add(c);
-
- //XElement tus = new XElement("TransportedUnits");
- //dto.UnitsTransported.ForEach(ut =>
- // {
- // XElement unit = new XElement("TransportedUnit");
- // unit.Add(new XAttribute("ID", ut));
- // tus.Add(unit);
- // });
- //x.Add(tus);
-
- return x;
- }
-
- public static XElement ToXML(this ICoordinateDTO dto)
- {
- XElement x = new XElement("Coordinate");
- x.Add(new XAttribute("X", dto.X));
- x.Add(new XAttribute("Y", dto.Y));
- x.Add(new XAttribute("Z", dto.Z));
-
- return x;
- }
-
- public static XElement ToXML(this ICountryDTO dto)
- {
- XElement x = new XElement("Country");
- x.Add(new XAttribute("ID", dto.ID));
- x.Add(new XAttribute("Name", dto.Name));
- x.Add(new XAttribute("Description", dto.Description));
- x.Add(new XAttribute("Faction", dto.Faction));
-
- return x;
- }
-
- public static XElement ToXML(this IFactionDTO dto)
- {
- XElement x = new XElement("Faction");
- x.Add(new XAttribute("ID", dto.ID));
- x.Add(new XAttribute("Name", dto.Name));
- x.Add(new XAttribute("Description", dto.Description));
-
- return x;
- }
-
- public static XElement ToXML(this IPlayerDTO dto)
- {
- XElement x = new XElement("Player");
- x.Add(new XAttribute("ID", dto.ID));
- x.Add(new XAttribute("Name", dto.Name));
- x.Add(new XAttribute("Description", dto.Description));
- x.Add(new XAttribute("Country", dto.Country));
- x.Add(new XAttribute("ReinforcementPoints", dto.ReinforcementPoints ?? 0));
- x.Add(new XAttribute("IsCurrentPlayer", dto.IsCurrentPlayer));
- x.Add(new XAttribute("IsAIPlayer", dto.IsAIPlayer));
-
- XElement unrfcmts = new XElement("UnplacedReinforcements");
- foreach (var i in dto.UnplacedReinforcements)
- {
- XElement unit = new XElement("Unit");
- unit.Add(new XAttribute("ID", i));
- unrfcmts.Add(unit);
- }
- x.Add(unrfcmts);
-
- return x;
- }
-
- public static XElement ToXML(this ITileDTO dto)
- {
- XElement x = new XElement("Tile");
- x.Add(new XAttribute("VictoryPoints", dto.VictoryPoints));
-
- XElement ds = new XElement("Demographics");
-
- dto.Demographics.ToList().ForEach(d =>
- {
- ds.Add(d.ToXML());
- });
-
- x.Add(ds);
-
- return x;
- }
-
- public static XElement ToXML(this IDemographicDTO dto)
- {
- XElement x = new XElement("Demographic");
- x.Add(new XAttribute("ID", dto.ID));
-
- return x;
- }
-
- public static XElement ToXML(this IScenarioDTO dto)
- {
- XElement x = new XElement("Scenario");
- x.Add(new XAttribute("ID", dto.ID));
- x.Add(new XAttribute("Name", dto.Name));
- x.Add(new XAttribute("GameFileDirectory", dto.GameFileDirectory));
- x.Add(new XAttribute("Author", dto.Author));
-
- return x;
- }
-
- public static XElement ToXML(this ISavedGameDTO dto)
- {
- XElement x = new XElement("SavedGame");
- x.Add(new XAttribute("ID", dto.ID));
- x.Add(new XAttribute("Name", dto.Name));
- x.Add(new XAttribute("GameFileDirectory", dto.GameFileDirectory));
- x.Add(new XAttribute("LastPlayed", dto.LastPlayed));
- x.Add(new XAttribute("Scenario", dto.Scenario));
-
- return x;
- }
-
- }
- }