Advertisement
imk0tter

XMLEntry

Oct 7th, 2013
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6. using System.Xml.Linq;
  7. namespace Delta_9_Test.Util
  8. {
  9.     public class XMLEntry
  10.     {
  11.         private string type = "";
  12.         private string text = "";
  13.  
  14.         private Dictionary<string, XMLEntry> children;
  15.  
  16.         private Dictionary<int, XMLEntry> group;
  17.         private Dictionary<string, string> attributes;
  18.  
  19.         public string Type
  20.         {
  21.             get { return type; }
  22.         }
  23.         public string Text
  24.         {
  25.             get { return text; }
  26.             set { text = value; }
  27.         }
  28.         public Dictionary<string, XMLEntry> Children
  29.         {
  30.             get
  31.             {
  32.                 return children;
  33.             }
  34.         }
  35.         public Dictionary<int, XMLEntry> Group
  36.         {
  37.             get
  38.             {
  39.                 return group;
  40.             }
  41.         }
  42.         public Dictionary<string, string> Attributes
  43.         {
  44.             get
  45.             {
  46.                 return attributes;
  47.             }
  48.         }
  49.  
  50.         public XMLEntry GetProperty(string name)
  51.         {
  52.             if (children.ContainsKey(name))
  53.                 return children[name];
  54.             return null;
  55.         }
  56.         public int GetPropertyCount()
  57.         {
  58.             return children.Count;
  59.         }
  60.  
  61.         public string GetAttribute(string name)
  62.         {
  63.             if (attributes.ContainsKey(name))
  64.                 return attributes[name];
  65.             return null;
  66.         }
  67.         public int GetAttributeCount()
  68.         {
  69.             return attributes.Count;
  70.         }
  71.  
  72.         public XMLEntry(XmlReader r)
  73.         {
  74.             this.children = new Dictionary<string, XMLEntry>();
  75.             this.group = new Dictionary<int, XMLEntry>();
  76.             this.attributes = new Dictionary<string, string>();
  77.             this.type = r.Name;
  78.  
  79.             ParseXML(r);
  80.             group.Add(group.Count, this);
  81.  
  82.         }
  83.         private void ParseXML(XmlReader r)
  84.         {
  85.             Stack<XMLEntry> stack = new Stack<XMLEntry>();
  86.             do
  87.             {
  88.                 switch (r.NodeType)
  89.                 {
  90.                     case XmlNodeType.Element:
  91.                         if (r.HasAttributes && r.Name == type)
  92.                         {
  93.                             for (int i = 0; i < r.AttributeCount; ++i)
  94.                             {
  95.                                 r.MoveToAttribute(i);
  96.                                 attributes.Add(r.Name, r.Value);  
  97.                             }
  98.                             r.MoveToElement();
  99.                         }
  100.                         if (r.Name != type)
  101.                         {
  102.                             if (children.ContainsKey(r.Name))
  103.                                 children[r.Name].AddGroup(new XMLEntry(r));
  104.                             else
  105.                                 children.Add(r.Name, new XMLEntry(r));
  106.                         }
  107.                         else
  108.                             if (r.IsEmptyElement)
  109.                                 return;
  110.                         break;
  111.                     case XmlNodeType.Text:
  112.                         this.text = r.Value;
  113.                         break;
  114.                     case XmlNodeType.EndElement:
  115.                         if (r.Name == type)
  116.                             return;
  117.                         else
  118.                             return;
  119.                     default:
  120.                         break;
  121.                 }
  122.             } while (r.Read());
  123.         }
  124.         public void AddGroup(XMLEntry i)
  125.         {
  126.             group.Add(group.Count, i);
  127.         }
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement