Advertisement
Guest User

Recursive XML dictionary wrapper

a guest
Dec 3rd, 2011
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.90 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7.  
  8. namespace CedLib
  9. {
  10.     namespace Persistence
  11.     {
  12.         namespace XMLPersistenceDictionary
  13.         {
  14.             using System.Xml.Serialization;
  15.  
  16.             [Serializable]
  17.             public class XMLPersistenceDictionary : ICollection, IEnumerable<savenode>
  18.             {
  19.                 public Logging logger;
  20.                
  21.                 public List<savenode> innerdict;
  22.  
  23.  
  24.                 public XMLPersistenceDictionary(Logging _logger)
  25.                 {
  26.                     logger = _logger;
  27.                     innerdict = new List<savenode>();
  28.                     logger.log("Initialized new XMLPersistence dictionary", Logging.Priority.Info);
  29.                 }
  30.  
  31.                 public XMLPersistenceDictionary()
  32.                 {
  33.                     innerdict = new List<savenode>();
  34.                 }
  35.  
  36.                 public bool ContainsKey(string key)
  37.                 {
  38.                     return innerdict.Exists(s => s.name == key);
  39.                 }
  40.                 public bool ContainsValue(string value)
  41.                 {
  42.                     return innerdict.Exists(s => s.obj == value);
  43.                 }
  44.                 public void Add(savenode value)
  45.                 {
  46.                     if (logger != null)
  47.                         logger.log("Adding new dictionary item: " + value.name, CedLib.Logging.Priority.Notice);
  48.                     innerdict.Add(value);
  49.                 }
  50.                 public void Add(string name, string obj)
  51.                 {
  52.                     savenode value = new savenode(name, obj);
  53.                     if (logger != null)
  54.                         logger.log("Adding new dictionary item: " + value.name, CedLib.Logging.Priority.Notice);
  55.                     innerdict.Add(value);
  56.                 }
  57.                 public void AddRange(savenode[] savenodes)
  58.                 {
  59.                     logger.log("Adding range of items", Logging.Priority.Notice);
  60.                     foreach (var savenode in savenodes)
  61.                     {
  62.                         if (logger != null)
  63.                             logger.log("Adding new dictionary item: " + savenode.name, CedLib.Logging.Priority.Notice);
  64.                         innerdict.Add(savenode);
  65.                     }
  66.                 }
  67.                 public void Remove(savenode value)
  68.                 {
  69.                     if (logger != null)
  70.                         logger.log("Removing dictionary item: " + value.name, CedLib.Logging.Priority.Notice);
  71.                     innerdict.Remove(value);
  72.                 }
  73.                 public void Remove(string value)
  74.                 {
  75.                     if (logger != null)
  76.                         logger.log("Removing dictionary item: " + value, CedLib.Logging.Priority.Notice);
  77.                     innerdict.Remove(innerdict.Find(s => s.name == value));
  78.                 }
  79.                 public savenode[] ToSavenodeArray()
  80.                 {
  81.                     return innerdict.ToArray();
  82.                 }
  83.                 public int Count
  84.                 {
  85.                     get { return innerdict.Count; }
  86.                 }
  87.  
  88.                 #region IEnumerable shit
  89.                 void ICollection.CopyTo(Array myArr, int index)
  90.                 {
  91.  
  92.                     foreach (savenode i in innerdict)
  93.                     {
  94.                         myArr.SetValue(i, index);
  95.                         index++;
  96.                     }
  97.  
  98.                 }
  99.  
  100.                 public IEnumerator<savenode> GetEnumerator()
  101.                 {
  102.                     return (IEnumerator<savenode>)innerdict.GetEnumerator();
  103.                 }
  104.                 IEnumerator IEnumerable.GetEnumerator()
  105.                 {
  106.                     // Defer to generic version
  107.                     return GetEnumerator();
  108.                 }
  109.  
  110.                 bool ICollection.IsSynchronized
  111.                 {
  112.                     get
  113.                     {
  114.                         return true;
  115.                     }
  116.                 }
  117.  
  118.                 object ICollection.SyncRoot
  119.                 {
  120.                     get
  121.                     {
  122.                         return this;
  123.                     }
  124.                 }
  125.  
  126.                 int ICollection.Count
  127.                 {
  128.                     get
  129.                     {
  130.                         return innerdict.Count;
  131.                     }
  132.                 }
  133.                 #endregion
  134.  
  135.  
  136.                 public savenode this[string index]
  137.                 {
  138.                     get { return innerdict.Find(s => s.name == index); }
  139.                     set
  140.                     {
  141.                         if (innerdict.Count > 0)
  142.                             innerdict[innerdict.FindIndex(s => s.name == index)] = value;
  143.                         else
  144.                             Add(value);
  145.                     }
  146.                 }
  147.                 public savenode this[int index]
  148.                 {
  149.                     get { return innerdict[index]; }
  150.                     set { innerdict[index] = value; }
  151.                 }
  152.  
  153.                 public bool save(string filename)
  154.                 {
  155.                     XmlSerializer serializer = new XmlSerializer(typeof(List<savenode>));
  156.                     System.IO.FileStream fstream = new System.IO.FileStream(filename, System.IO.FileMode.OpenOrCreate);
  157.                     serializer.Serialize(fstream, innerdict);
  158.                     fstream.Flush();
  159.                     fstream.Close();
  160.                     return true;
  161.                 }
  162.  
  163.                 public bool load(string filename)
  164.                 {
  165.                     XmlSerializer serializer = new XmlSerializer(typeof(List<savenode>));
  166.                     System.IO.FileStream fstream = new System.IO.FileStream(filename, System.IO.FileMode.Open);
  167.                     innerdict = (List<savenode>)serializer.Deserialize(fstream);
  168.                     fstream.Flush();
  169.                     fstream.Close();
  170.                     return true;
  171.                 }
  172.             }
  173.  
  174.  
  175.             [Serializable]
  176.             public class savenode
  177.             {
  178.                 public savenode(string _name, string _object)
  179.                 {
  180.                     name = _name;
  181.                     obj = _object;
  182.                     Console.WriteLine("New node: " + _name);
  183.                 }
  184.                 public savenode(string _name, string _object, XMLPersistenceDictionary _childnodes)
  185.                 {
  186.                     name = _name;
  187.                     obj = _object;
  188.                     childnodes = _childnodes;
  189.                     Console.WriteLine("New node: " + _name);
  190.                 }
  191.                 public savenode() { }
  192.  
  193.                 public override string ToString()
  194.                 {
  195.                     return name + ":" + obj;
  196.                 }
  197.  
  198.                 public void Add(object snode)
  199.                 {
  200.                     if (childnodes == null)
  201.                         childnodes = new XMLPersistenceDictionary();
  202.                     childnodes.Add((savenode)snode);
  203.                 }
  204.                 public void Add(savenode snode)
  205.                 {
  206.                     if (childnodes == null)
  207.                         childnodes = new XMLPersistenceDictionary();
  208.                     childnodes.Add(snode);
  209.                 }
  210.                 public void Add(string name,string obj)
  211.                 {
  212.                     if (childnodes == null)
  213.                         childnodes = new XMLPersistenceDictionary();
  214.                     childnodes.Add(name,obj);
  215.                 }
  216.  
  217.  
  218.  
  219.                 public string name { get; set; }
  220.                
  221.                 public string obj { get; set; }
  222.  
  223.  
  224.                 [XmlArray("childnodes")]
  225.                 public XMLPersistenceDictionary childnodes;
  226.             }
  227.         }
  228.  
  229.  
  230.  
  231.  
  232.     }
  233. }
  234.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement