Advertisement
MarMar_IV

Generic XMLgateway

Nov 6th, 2015
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Xml;
  10. using AR.Entities;
  11. using AR.Gateways.XML;
  12.  
  13. namespace AR.Gateways
  14. {
  15.     public class UniversalGateway_XML<T> : IGateway<T> where T : BaseEntity
  16.     {
  17.         private XmlDocument document;
  18.         private string mainName;
  19.         private string mainListString;
  20.         private XmlNode mainList;
  21.         private string databaseFile;
  22.  
  23.         public UniversalGateway_XML(string p_databaseDirectory) {
  24.             this.mainName = typeof(T).Name;
  25.             this.mainListString = this.mainName + "List";
  26.             this.databaseFile = p_databaseDirectory.TrimEnd('\\') + @"\" + mainName + "s.xml";
  27.  
  28.             if (!File.Exists(this.databaseFile))
  29.             {
  30.                 using (FileStream fs = File.Create(this.databaseFile))
  31.                 {
  32.                     using (StreamWriter sw = new StreamWriter(fs))
  33.                     {
  34.                         sw.WriteLine("<?xml version=\"1.0\"?><" + this.mainListString + "></" + this.mainListString + ">");
  35.                     }
  36.                 }
  37.             }
  38.  
  39.             this.document = new XmlDocument();
  40.             this.document.Load(this.databaseFile);
  41.  
  42.             this.mainList = this.document.GetElementsByTagName(this.mainListString)[0];
  43.         }
  44.  
  45.         private void saveXML()
  46.         {
  47.             this.document.Save(this.databaseFile);
  48.         }
  49.  
  50.         private int getNextID()
  51.         {
  52.             int res = 1;
  53.  
  54.             try
  55.             {
  56.                 XmlNodeList entitys = this.mainList.ChildNodes;
  57.                 foreach (XmlNode entit in entitys)
  58.                 {
  59.                     int lastID = -1;
  60.                     try
  61.                     {
  62.                         PropertyInfo[] ppi = typeof(BaseEntity).GetProperties();
  63.                         lastID = int.Parse(entit[ppi[0].Name].InnerText);
  64.                     }
  65.                     catch (Exception e)
  66.                     {
  67.                         res = -1;
  68.                         break;
  69.                     }
  70.  
  71.                     if (lastID > -1)
  72.                     {
  73.                         if (lastID >= res)
  74.                         {
  75.                             res = lastID + 1;
  76.                         }
  77.                     }
  78.                 }
  79.             }
  80.             catch (Exception e)
  81.             {
  82.                 res = -1;
  83.             }
  84.  
  85.             return res;
  86.         }
  87.  
  88.         public IEnumerable<T> List
  89.         {
  90.             get
  91.             {
  92.                 throw new NotImplementedException();
  93.             }
  94.         }
  95.  
  96.         public bool Add(T entity)
  97.         {
  98.             bool res = false;
  99.  
  100.             try
  101.             {
  102.                 XmlNode entityNode = this.document.CreateNode(XmlNodeType.Element, this.mainName, "");
  103.                 int newID = this.getNextID();
  104.                 if (newID > -1)
  105.                 {
  106.                     entity.Id = newID;
  107.                     foreach (PropertyInfo propertyInfo in typeof(User).GetProperties())
  108.                     {
  109.                         XmlNode node_property = this.document.CreateNode(XmlNodeType.Element, propertyInfo.Name, "");
  110.                         node_property.InnerText = propertyInfo.GetValue(entity, null).ToString();
  111.                         entityNode.AppendChild(node_property);
  112.                     }
  113.                     this.mainList.AppendChild(entityNode);
  114.  
  115.                     this.saveXML();
  116.                     res = true;
  117.                 }
  118.             }
  119.             catch (Exception e)
  120.             {
  121.                 res = false;
  122.             }
  123.  
  124.             return res;
  125.         }
  126.  
  127.         public bool Delete(T entity)
  128.         {
  129.             throw new NotImplementedException();
  130.         }
  131.  
  132.         public T FindById(int Id)
  133.         {
  134.             throw new NotImplementedException();
  135.         }
  136.  
  137.         public IQueryable<T> SearchFor(Expression<Func<T, bool>> predicate)
  138.         {
  139.             throw new NotImplementedException();
  140.         }
  141.  
  142.         public bool Update(T entity)
  143.         {
  144.             throw new NotImplementedException();
  145.         }
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement