شركة التطبيقات المتكاملة لتصميم النظم البرمجية الخاصة

Integrated Applications Programming Company

Skip Navigation LinksHome » Code Library » Xml

Public general use code classes and xml files that we've compiled and used over the years:

XML Database support class. This handles storing and retrieving an XDocument, DataSet and Text storage.

   1:  using System;
   2:  using System.Data;
   3:  using System.Xml.Linq;
   4:   
   5:  namespace Ia.Cl.Model.Db
   6:  {
   7:      ////////////////////////////////////////////////////////////////////////////
   8:   
   9:      /// <summary publish="true">
  10:      /// XML Database support class. This handles storing and retrieving an XDocument, DataSet and Text storage.
  11:      /// </summary>
  12:      /// <remarks> 
  13:      /// Copyright © 2001-2015 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
  14:      ///
  15:      /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
  16:      /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  17:      ///
  18:      /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  19:      /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  20:      /// 
  21:      /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  22:      /// 
  23:      /// Copyright notice: This notice may not be removed or altered from any source distribution.
  24:      /// </remarks> 
  25:   
  26:      /*
  27:       *  Sample
  28:       *  ////////////////////////////////////////////////////////////////////////////
  29:       *  
  30:       * /// <summary>
  31:         ///
  32:         /// </summary>
  33:         public static void StoreContent(string name, string content)
  34:         {
  35:             global::Ia.Cl.Model.Db.Xml n = new global::Ia.Cl.Model.Db.Xml(name);
  36:  
  37:             n.Text = content;
  38:  
  39:             n.SaveText();
  40:         }
  41:  
  42:         ////////////////////////////////////////////////////////////////////////////
  43:  
  44:         /// <summary>
  45:         ///
  46:         /// </summary>
  47:         public static string ReadContent(string name)
  48:         {
  49:             global::Ia.Cl.Model.Db.Xml n = new global::Ia.Cl.Model.Db.Xml(name);
  50:  
  51:             return n.Text;
  52:         }
  53:      */
  54:   
  55:      public class Xml
  56:      {
  57:          private string fileName, filePath;
  58:          private DataSet ds;
  59:          private XDocument xd;
  60:          private string text;
  61:   
  62:          ////////////////////////////////////////////////////////////////////////////
  63:   
  64:          /// <summary>
  65:          ///
  66:          /// </summary>
  67:          public Xml()
  68:          {
  69:              fileName = "default";
  70:   
  71:              filePath = Path() + fileName;
  72:          }
  73:   
  74:          ////////////////////////////////////////////////////////////////////////////
  75:   
  76:          /// <summary>
  77:          ///
  78:          /// </summary>
  79:          public Xml(string _fileName)
  80:          {
  81:              fileName = _fileName;
  82:   
  83:              filePath = Path() + fileName;
  84:          }
  85:   
  86:          ////////////////////////////////////////////////////////////////////////////
  87:   
  88:          /// <summary>
  89:          ///
  90:          /// </summary>
  91:          public XDocument XDocument
  92:          {
  93:              get
  94:              {
  95:                  if (xd == null) ReadXDocument();
  96:   
  97:                  return xd;
  98:              }
  99:   
 100:              set
 101:              {
 102:                  xd = value;
 103:   
 104:                  SaveXDocument();
 105:              }
 106:          }
 107:   
 108:          ////////////////////////////////////////////////////////////////////////////
 109:   
 110:          /// <summary>
 111:          ///
 112:          /// </summary>
 113:          public DataSet DataSet
 114:          {
 115:              get
 116:              {
 117:                  if (ds == null) ReadDataSet();
 118:   
 119:                  return ds;
 120:              }
 121:   
 122:              set
 123:              {
 124:                  ds = value;
 125:   
 126:                  SaveDataSet();
 127:              }
 128:          }
 129:   
 130:          ////////////////////////////////////////////////////////////////////////////
 131:   
 132:          /// <summary>
 133:          ///
 134:          /// </summary>
 135:          public string Text
 136:          {
 137:              get
 138:              {
 139:                  if (text == null) ReadText();
 140:   
 141:                  return text;
 142:              }
 143:   
 144:              set
 145:              {
 146:                  text = value;
 147:   
 148:                  SaveText();
 149:              }
 150:          }
 151:   
 152:          ////////////////////////////////////////////////////////////////////////////
 153:   
 154:          /// <summary>
 155:          ///
 156:          /// </summary>
 157:          public bool ReadXDocument()
 158:          {
 159:              bool b;
 160:              string r;
 161:   
 162:              xd = null;
 163:   
 164:              try
 165:              {
 166:                  if (File.Exists(filePath + ".xdocument.xml"))
 167:                  {
 168:                      xd = XDocument.Load(filePath + ".xdocument.xml");
 169:                  }
 170:                  else
 171:                  {
 172:                      xd = new XDocument();
 173:   
 174:                      xd.Save(filePath + ".xdocument.xml");
 175:                  }
 176:   
 177:                  b = true;
 178:              }
 179:              catch (Exception ex)
 180:              {
 181:                  b = false;
 182:  #if DEBUG
 183:                  r = "Error: " + ex.ToString();
 184:  #else
 185:                  r = "Error: " + ex.Message;
 186:  #endif
 187:              }
 188:              finally
 189:              {
 190:              }
 191:   
 192:              return b;
 193:          }
 194:   
 195:          ////////////////////////////////////////////////////////////////////////////
 196:   
 197:          /// <summary>
 198:          ///
 199:          /// </summary>
 200:          public bool ReadDataSet()
 201:          {
 202:              bool b;
 203:              string r;
 204:   
 205:              try
 206:              {
 207:                  if (File.Exists(filePath + ".dataset.xml"))
 208:                  {
 209:                      ds.ReadXml(filePath + ".dataset.xml", XmlReadMode.ReadSchema);
 210:                  }
 211:                  else
 212:                  {
 213:                      ds = new DataSet(fileName);
 214:   
 215:                      ds.WriteXml(filePath + ".dataset.xml", XmlWriteMode.WriteSchema);
 216:                  }
 217:   
 218:                  b = true;
 219:              }
 220:              catch (Exception ex)
 221:              {
 222:                  b = false;
 223:  #if DEBUG
 224:                  r = "Error: " + ex.ToString();
 225:  #else
 226:                  r = "Error: " + ex.Message;
 227:  #endif
 228:              }
 229:              finally
 230:              {
 231:              }
 232:   
 233:              return b;
 234:          }
 235:   
 236:          ////////////////////////////////////////////////////////////////////////////
 237:   
 238:          /// <summary>
 239:          ///
 240:          /// </summary>
 241:          public bool ReadText()
 242:          {
 243:              bool b;
 244:              string r;
 245:   
 246:              text = null;
 247:   
 248:              try
 249:              {
 250:                  if (File.Exists(filePath + ".text.xml"))
 251:                  {
 252:                      text = System.IO.File.ReadAllText(filePath + ".text.xml");
 253:                  }
 254:                  else
 255:                  {
 256:                      System.IO.File.WriteAllText(filePath + ".text.xml", text);
 257:                  }
 258:   
 259:                  b = true;
 260:              }
 261:              catch (Exception ex)
 262:              {
 263:                  b = false;
 264:  #if DEBUG
 265:                  r = "Error: " + ex.ToString();
 266:  #else
 267:                  r = "Error: " + ex.Message;
 268:  #endif
 269:              }
 270:              finally
 271:              {
 272:              }
 273:   
 274:              return b;
 275:          }
 276:   
 277:          ////////////////////////////////////////////////////////////////////////////
 278:   
 279:          /// <summary>
 280:          ///
 281:          /// </summary>
 282:          public bool SaveXDocument()
 283:          {
 284:              bool b;
 285:              string r;
 286:   
 287:              try
 288:              {
 289:                  xd.Save(filePath + ".xdocument.xml");
 290:   
 291:                  b = true;
 292:              }
 293:              catch (Exception ex)
 294:              {
 295:                  b = false;
 296:  #if DEBUG
 297:                  r = "Error: " + ex.ToString();
 298:  #else
 299:                  r = "Error: " + ex.Message;
 300:  #endif
 301:              }
 302:              finally
 303:              {
 304:              }
 305:   
 306:              return b;
 307:          }
 308:   
 309:          ////////////////////////////////////////////////////////////////////////////
 310:          ////////////////////////////////////////////////////////////////////////////
 311:   
 312:          /// <summary>
 313:          ///
 314:          /// </summary>
 315:          public bool SaveDataSet()
 316:          {
 317:              bool b;
 318:              string r;
 319:   
 320:              try
 321:              {
 322:                  ds.WriteXml(filePath + ".dataset.xml", XmlWriteMode.WriteSchema);
 323:   
 324:                  b = true;
 325:              }
 326:              catch (Exception ex)
 327:              {
 328:                  b = false;
 329:  #if DEBUG
 330:                  r = "Error: " + ex.ToString();
 331:  #else
 332:                  r = "Error: " + ex.Message;
 333:  #endif
 334:              }
 335:              finally
 336:              {
 337:              }
 338:   
 339:              return b;
 340:          }
 341:   
 342:          ////////////////////////////////////////////////////////////////////////////
 343:   
 344:          /// <summary>
 345:          ///
 346:          /// </summary>
 347:          public bool SaveText()
 348:          {
 349:              bool b;
 350:              string r;
 351:   
 352:              try
 353:              {
 354:                  System.IO.File.WriteAllText(filePath + ".text.xml", text);
 355:   
 356:                  b = true;
 357:              }
 358:              catch (Exception ex)
 359:              {
 360:                  b = false;
 361:  #if DEBUG
 362:                  r = "Error: " + ex.ToString();
 363:  #else
 364:                  r = "Error: " + ex.Message;
 365:  #endif
 366:              }
 367:              finally
 368:              {
 369:              }
 370:   
 371:              return b;
 372:          }
 373:   
 374:          ////////////////////////////////////////////////////////////////////////////
 375:   
 376:          /// <summary>
 377:          ///
 378:          /// </summary>
 379:          private string DateTime(DateTime dt)
 380:          {
 381:              // return a friendly string of a datetime value
 382:              string s;
 383:   
 384:              s = dt.ToString("yyyy-MM-dd HH:mm:ss");
 385:   
 386:              return s;
 387:          }
 388:   
 389:          ////////////////////////////////////////////////////////////////////////////
 390:   
 391:          /// <summary>
 392:          ///
 393:          /// </summary>
 394:          private string Path()
 395:          {
 396:              string path;
 397:   
 398:  #if WFA
 399:              if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed) path = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.DataDirectory + @"\";
 400:              else path = AppDomain.CurrentDomain.BaseDirectory;
 401:  #else
 402:              path = AppDomain.CurrentDomain.BaseDirectory.ToString() + @"app_data\";
 403:  #endif
 404:   
 405:              return path;
 406:          }
 407:   
 408:          ////////////////////////////////////////////////////////////////////////////
 409:          ////////////////////////////////////////////////////////////////////////////
 410:      }
 411:   
 412:      ////////////////////////////////////////////////////////////////////////////
 413:      ////////////////////////////////////////////////////////////////////////////
 414:  }
 415: