Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class XMLEntry
- {
- private int id = -1;
- private string name = "";
- private string type = "";
- private string text;
- private Dictionary<string, XMLEntry> children;
- private Dictionary<int, XMLEntry> group;
- private Dictionary<string, string> attributes;
- public XMLEntry(XmlReader r)
- {
- this.children = new Dictionary<string, XMLEntry>();
- this.group = new Dictionary<int, XMLEntry>();
- this.attributes = new Dictionary<string, string>();
- this.type = r.Name;
- if (type == "Object" || type == "Ground")
- {
- id = Convert.ToInt32(r.GetAttribute("type"), 16);
- name = r.GetAttribute("id");
- }
- ParseXML(r);
- group.Add(group.Count, this);
- }
- public XMLEntry(string name)
- {
- this.type = name;
- }
- private void ParseXML(XmlReader r)
- {
- Stack<XMLEntry> stack = new Stack<XMLEntry>();
- do
- {
- switch (r.NodeType)
- {
- case XmlNodeType.Element:
- if (r.HasAttributes && r.Name == type)
- {
- Console.WriteLine("Setting '{0}'='{2}' attributes: Quantity {1}", type, r.AttributeCount, name);
- for (int i = 0; i < r.AttributeCount; ++i)
- {
- r.MoveToAttribute(i);
- Console.WriteLine("Adding attribute '{0}' of value '{1}'", r.Name, r.Value);
- attributes.Add(r.Name, r.Value);
- }
- r.MoveToElement();
- }
- if (r.Name != type)
- {
- if (children.ContainsKey(r.Name))
- {
- Console.WriteLine("Adding '{0}' to group dictionary of '{1}'", r.Name, type);
- children[r.Name].AddGroup(new XMLEntry(r));
- }
- else
- {
- Console.WriteLine("Adding '{0}' to children dictionary of '{1}'", r.Name, type);
- children.Add(r.Name, new XMLEntry(r));
- }
- }
- else
- {
- if (r.IsEmptyElement)
- return;
- }
- break;
- case XmlNodeType.Text:
- Console.WriteLine("Setting text of {0} to {1}", type, r.Value);
- this.text = r.Value;
- break;
- case XmlNodeType.EndElement:
- if (r.Name == type)
- {
- Console.WriteLine("Finalizing element: '{0}'=''", type, name);
- return;
- }
- else
- {
- Console.Write("Weird stuff happuned");
- return;
- }
- break;
- default:
- break;
- }
- } while (r.Read());
- }
- public int GroupCount()
- {
- return group.Count;
- }
- public XMLEntry GetGroup(int id)
- {
- if (group.ContainsKey(id))
- return group[id];
- return null;
- }
- public bool Contains(string type)
- {
- if (children.ContainsKey(type))
- return true;
- return false;
- }
- public void AddGroup(XMLEntry i)
- {
- group.Add(group.Count, i);
- }
- public void AddChild(string type, XMLEntry i)
- {
- this.children.Add(type, i);
- }
- public XMLEntry GetProperty(string type)
- {
- if (children.ContainsKey(type))
- return children[type];
- return null;
- }
- public int GetPropertyCount()
- {
- return children.Count;
- }
- public string Type
- {
- get { return type; }
- }
- public string Name
- {
- get { return name; }
- }
- public string Text
- {
- get { return text; }
- set { text = value; }
- }
- public int ID
- {
- get { return id; }
- }
- }
- public static class ObjectLibrary
- {
- public static Dictionary<int, XMLEntry> ObjectDictionary = new Dictionary<int, XMLEntry>();
- public static Dictionary<int, XMLEntry> GroundDictionary = new Dictionary<int, XMLEntry>();
- public static void ProcessXMLFiles(string path)
- {
- string[] xmlFiles = Directory.GetFiles(@path, "*.xml");
- foreach (string s in xmlFiles)
- {
- Console.WriteLine("Processing object XML File: {0}", s);
- XmlReader reader = XmlReader.Create(s);
- while (reader.Read())
- {
- switch (reader.NodeType)
- {
- case XmlNodeType.Element:
- if (reader.Name == "Object")
- {
- XMLEntry ent = new XMLEntry(reader);
- try
- {
- ObjectDictionary.Add(ent.ID, ent);
- }
- catch (Exception e)
- {
- Console.WriteLine("================= Element {1} is already present in the dictionary {0}====================", ObjectDictionary[ent.ID].Name, ent.ID);
- }
- }
- break;
- default:
- Console.WriteLine("Skipped Node type: {0}", reader.NodeType);
- break;
- }
- }
- }
- xmlFiles = Directory.GetFiles(@(path + "GroundObjects/"));
- foreach (string s in xmlFiles)
- {
- Console.WriteLine("Processing ground object file: {0}", s);
- XmlReader r = XmlReader.Create(s);
- while (r.Read())
- {
- switch(r.NodeType)
- {
- case XmlNodeType.Element:
- if (r.Name == "Object" || r.Name == "Ground")
- {
- XMLEntry ent = new XMLEntry(r);
- try
- {
- ObjectDictionary.Add(ent.ID, ent);
- }
- catch (Exception e)
- {
- Console.WriteLine("================= Element {1} is already present in the dictionary {0}====================", ObjectDictionary[ent.ID].Name, ent.ID);
- }
- }
- break;
- default:
- Console.WriteLine("Skipped Node type: {0}", r.NodeType);
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement