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

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