public void Serialize(IRoadmap roadmap, string outFilePath) { using (FileStream fileStream = File.Open(outFilePath, FileMode.Create)) { XmlDocument xml = new XmlDocument(); XmlElement root = xml.CreateElement("roadmap"); xml.AppendChild(root); var splinesNode = xml.CreateElement("roadSplines"); for (var i = 0; i < roadmap.Splines.Count; i++) { var splineNode = xml.CreateElement("roadSpline"); var indexAttribute = xml.CreateAttribute("index"); indexAttribute.Value = i.ToString(); splineNode.Attributes.Append(indexAttribute); WriteSpline(roadmap.Splines[i], xml, splineNode); splinesNode.AppendChild(splineNode); } root.AppendChild(splinesNode); var roadsNode = xml.CreateElement("roads"); for (var i = 0; i < roadmap.Roads.Count; i++) { var roadNode = xml.CreateElement("road"); var indexAttribute = xml.CreateAttribute("index"); indexAttribute.Value = i.ToString(); roadNode.Attributes.Append(indexAttribute); var fromCrossroad = roadmap.RoadToCrossroadMapping[i * 2]; var toCrossroad = roadmap.RoadToCrossroadMapping[i * 2 + 1]; WriteRoad(roadmap.Roads[i], fromCrossroad, toCrossroad, xml, roadNode); roadsNode.AppendChild(roadNode); } root.AppendChild(roadsNode); var crossroadsNode = xml.CreateElement("crossroads"); for (var i = 0; i < roadmap.Crossroads.Count; i++) { var crossroadNode = xml.CreateElement("crossroad"); var indexAttribute = xml.CreateAttribute("index"); indexAttribute.Value = i.ToString(); crossroadNode.Attributes.Append(indexAttribute); WriteCrossroad(roadmap.Crossroads[i], xml, crossroadNode); crossroadsNode.AppendChild(crossroadNode); } root.AppendChild(crossroadsNode); var roadGraphNode = xml.CreateElement("roadGraph"); for (var i = 0; i < roadmap.CostMap.Count; i++) { var roadGraphEdgeNode = xml.CreateElement("roadGraphEdge"); var costMapping = roadmap.CostMap[i]; roadGraphEdgeNode.AddAttrWithValue(xml, "index", i.ToString()); roadGraphEdgeNode.AddFieldWithValue(xml, "edgeType", costMapping.RoadGraphEdgeType.ToString()); roadGraphEdgeNode.AddFieldWithValue(xml, "cost", costMapping.Cost.ToString()); if (costMapping.RoadGraphEdgeType == RoadGraphEdgeType.Road) { roadGraphEdgeNode.AddFieldWithValue(xml, "roadIndex", costMapping.RoadGraphEdgeLink.ToString()); var roadIndex = roadmap.GetRoadIndexForRoadGraphEdge(i); if (roadIndex != costMapping.RoadGraphEdgeLink) { throw new InvalidDataException($"Unexpected road index mismatch: {costMapping.RoadGraphEdgeLink} != {roadIndex}"); } } else if (costMapping.RoadGraphEdgeType == RoadGraphEdgeType.CrossroadJunction) { roadGraphEdgeNode.AddFieldWithValue(xml, "crossroadIndex", costMapping.RoadGraphEdgeLink.ToString()); roadGraphEdgeNode.AddFieldWithValue(xml, "targetRoadIndex", roadmap.GetRoadIndexForRoadGraphEdge(i).ToString()); } roadGraphNode.AppendChild(roadGraphEdgeNode); } root.AppendChild(roadGraphNode); xml.Save(fileStream); } }
public IRoadmap Deserialize(IRoadmapFactory roadmapFactory, string filePath) { IRoadmap roadmap = roadmapFactory.Roadmap(); using (FileStream fileStream = File.Open(filePath, FileMode.Open)) { XmlDocument xml = new XmlDocument(); xml.Load(fileStream); XmlElement root = xml.DocumentElement; var roadSplinesNode = root.SelectSingleNode("roadSplines"); foreach (XmlNode splineNode in roadSplinesNode.ChildNodes) { if (splineNode.Name == "roadSpline") { IRoadSpline spline = roadmapFactory.Spline(); ReadSpline(spline, splineNode); roadmap.Splines.Add(spline); } } var roadNodes = root.SelectNodes("roads/road"); foreach (XmlNode roadNode in roadNodes) { IRoadDefinition road = roadmapFactory.Road(); ReadRoad(road, roadmapFactory, roadNode); roadmap.Roads.Add(road); var fromCrossroad = ushort.Parse(roadNode.GetAttrValue("fromCrossroad")); var toCrossroad = ushort.Parse(roadNode.GetAttrValue("toCrossroad")); roadmap.RoadToCrossroadMapping.Add(fromCrossroad); roadmap.RoadToCrossroadMapping.Add(toCrossroad); } var crossroadNodes = root.SelectNodes("crossroads/crossroad"); foreach (XmlNode crossroadNode in crossroadNodes) { ICrossroad crossroad = roadmapFactory.Crossroad(); ReadCrossroad(crossroad, roadmapFactory, crossroadNode); roadmap.Crossroads.Add(crossroad); } var roadGraphEdgeNodes = root.SelectNodes("roadGraph/roadGraphEdge"); foreach (XmlNode edgeNode in roadGraphEdgeNodes) { ICostMapEntry costMapEntry = roadmapFactory.CostMapEntry(); roadmap.CostMap.Add(costMapEntry); var type = (RoadGraphEdgeType)Enum.Parse(typeof(RoadGraphEdgeType), edgeNode.GetFieldValue("edgeType")); var cost = ushort.Parse(edgeNode.GetFieldValue("cost")); costMapEntry.RoadGraphEdgeType = type; costMapEntry.Cost = cost; if (type == RoadGraphEdgeType.Road) { var roadIndex = ushort.Parse(edgeNode.GetFieldValue("roadIndex")); costMapEntry.RoadGraphEdgeLink = roadIndex; roadmap.AddRoadEdge(roadIndex); } else if (type == RoadGraphEdgeType.CrossroadJunction) { var crossroadIndex = ushort.Parse(edgeNode.GetFieldValue("crossroadIndex")); var targetRoadIndex = ushort.Parse(edgeNode.GetFieldValue("targetRoadIndex")); costMapEntry.RoadGraphEdgeLink = crossroadIndex; roadmap.AddCrossroadJunctionEdge(targetRoadIndex); } } } return(roadmap); }