Game-Design / Sprint3_actual / XML_LevelLoading / XMLParser.cs
XMLParser.cs
Raw
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Sprint3_actual.XML_LevelLoading
{
    public class XMLParser
    {
        public struct Item
        {
            public String objectType;
            public int objectName;
            public int x;
            public int y;

            public static implicit operator Item(int val)
            {
                return new Item() { objectType = "", objectName = 1, x = -1, y = -1 };
            }
        }

        private List<Item> items;
        private XmlDocument doc;
        private XmlNodeList nodeList;

        public XMLParser(string filePath)
        {
            doc = new XmlDocument();
            doc.Load(filePath);

            nodeList = doc.SelectNodes("/XnaContent/Asset/Item");
            items = new List<Item>();
        }

        public List<Item> parse()
        {
            foreach(XmlNode item in nodeList)
            {
                Item obj = new Item();

                obj.objectType = item.ChildNodes[0].InnerText;
                obj.objectName = int.Parse(item.ChildNodes[1].InnerText);

                string location = item.ChildNodes[2].InnerText;
                int space = location.IndexOf(" ");
                obj.x = int.Parse(location.Substring(0,space));
                obj.y = int.Parse(location.Substring(space+1));
                obj.y += 168;

                items.Add(obj);
            }

            return items;
        }
    }
}