Exemplo n.º 1
0
        public static Map Parse(string filename)
        {
            Debug.Assert(filename != null);

            var doc = new XmlDocument();

            doc.Load(filename);
            var root = doc.DocumentElement;

            Debug.Assert(root != null);
            if (root.Name != ElementMap)
            {
                throw new Exception(Resources.InvalidMapFileError);
            }

            var version = root.GetAttribute(AttrVersion);

            if (version != SupportedVersion)
            {
                throw new Exception(Resources.UnsupportedVersionError);
            }
            var name = root.GetAttribute(AttrName);

            var floors        = new List <Floor>();
            var floorElements = root.SelectNodes(ElementFloor)?.OfType <XmlElement>();

            if (floorElements == null)
            {
                return(new Map(name, floors));
            }
            foreach (var floorElement in floorElements)
            {
                var floor = new Floor();

                var entryNodeElements = floorElement.SelectNodes($"Node[@{AttrType}='{NodeType.GuideNode}']")?.OfType <XmlElement>();
                Debug.Assert(entryNodeElements != null);
                floor.AddNodes(entryNodeElements.Select(GenerateNode));

                var wallNodeElements = floorElement.SelectNodes($"Node[@{AttrType}='{NodeType.WallNode}']")?.OfType <XmlElement>();
                Debug.Assert(wallNodeElements != null);
                floor.AddNodes(wallNodeElements.Select(GenerateNode));

                var linkElements = floorElement.SelectNodes(ElementLink)?.OfType <XmlElement>();
                Debug.Assert(linkElements != null);
                floor.AddLinks(linkElements.Select(GenerateLink));

                floors.Add(floor);
            }

            return(new Map(name, floors));
        }