Tag Archive for store

Store/retrieve object in session

//On Login page
User currentUser = <get the User entity here, including relations to UserRights>;
Session.Add("CurrentUser", currentUser);

//On another page
User restoredUser = Session["CurrentUser"] as User;
if (restoredUser != null)
{
	//use the User entity here to check user's rights
}

source

AS3 Use Dictionary Obect to Store Values Associated with MovieClips

// This example assumes there are 10 movieClips on stage named btn1, btn2, btn3 ... etc

import flash.utils.Dictionary;

var btnDict:Dictionary = new Dictionary();

for (var i:uint=1; i<=10; i++) {
	var tempBtn:MovieClip = this["btn"+i];
	tempBtn.addEventListener(MouseEvent.CLICK, onBtn_CLICK);
	tempBtn.buttonMode = true;
	tempBtn.mouseChildren = false;
	btnDict[tempBtn] = i;
}

function onBtn_CLICK(event:MouseEvent):void {
	trace(btnDict[event.target]);
}

source

The jQuery Data Store

//Store a color
$(this).data('color', $(this).css('color'));
//Retrieve the color later
$(this).css('color', $(this).data('color'));
//Remove the data
$(this).removeData('color');

source

xml handler

/**
* XmlHandler.NET
* Version: Beta 1.0
* Released: 10 Dec, 2007
* Core Developer:Jegatheeswaran(JScriptLover@gmail.com)
* Credits:Vasanthakumar(DrunkenProgrammer@gmail.com), Syed Vaisul Karne(SqlLover@gmail.com)
* Copyright (C) 2007

* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Lesser General Public License for more details.

* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**/

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Data;
using System.Web;

namespace ideaBubbling.Xml
{
/// <summary>
/// A table type xml file can be handled easily through this XmlHandler class
/// </summary>
public class XmlHandler
{
#region Private Memebers
private string _RootElement;
private string _SubRootElement;
private string _XmlFileName;
private string[] _XmlFieldNames;
private Type[] _XmlFieldTypes;
private string _XmlKeyField;
private DataSet _DSXml;
private XmlDocument _XmlDoc;
private int _RecordCount;
private int _FieldCount;
private bool _FileAutoGenerated;
bool _IsIdentity;
bool _IsKeyField;
#endregion

#region Properties
public string File
{
get
{
return _XmlFileName;
}
set
{
this.Commit();
_XmlFileName = value;
if (IsValidFile())
{
if (_DSXml.Tables.Count > 0)
_RecordCount = _DSXml.Tables[0].Rows.Count;
else
_RecordCount = 0;
}
}
}
public string RootElement
{
get
{
return _RootElement;
}
}
public string SubRootElement
{
get
{
return _SubRootElement;
}
}
public string[] FieldNames
{
get
{
return _XmlFieldNames;
}
}
//Count is renamed as RecordCount by Syed.
public int RecordCount
{
get
{
return _RecordCount;
}
}
#endregion

#region Indexers
public DataRow this[int pIndex]
{
get
{
return _DSXml.Tables[0].Rows[pIndex];
}
}
#endregion

#region Constructors
private XmlHandler(){}

/// <summary>
/// To initialize the xml file with its structure
/// </summary>
/// <param name="pXmlFile">Xml file to be handled (Name with full path)</param>
/// <param name="pFileAutoGenerated">If it is true, then the given xml file will be created if the file does not exist</param>
/// <param name="pXmlStructure">Structure of the xml file.first "Root Element", then "Record element", then "multiple Field elements" ex:&lt;Students&gt;&lt;Student&gt;&lt;Name/&gt;&lt;Age/&gt;&lt;/Student&gt;&lt;/Students&gt;</param>
public XmlHandler(string pXmlFile, bool pFileAutoGenerated, string pXmlStructure)
{
try
{
DataSet D = new DataSet();
D = ConvertXMLToDataSet(pXmlStructure);
if(D.Tables.Count!=1)
throw new Exception("Invalid xml structure");
string[] fieldNames=new string[D.Tables[0].Columns.Count];
for(int i=0;i<D.Tables[0].Columns.Count;i++)
fieldNames.SetValue(D.Tables[0].Columns[i].ColumnName, i);
_FileAutoGenerated = pFileAutoGenerated;
this.ConfigureXmlHandler(pXmlFile, D.DataSetName, D.Tables[0].TableName, fieldNames);
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// To initialize the xml file with its root element, sub-root element and fields
/// </summary>
/// <param name="pXmlFile">Xml file to be handled (Name with full path)</param>
/// <param name="pFileAutoGenerated">If it is true, then the given xml file will be created if the file does not exist</param>
/// <param name="pRootElement">Root element of the xml file</param>
/// <param name="pSubRootElement">Sub-Root element under the root element of the xml file</param>
/// <param name="pFieldNames">Child tags' name under the sub-root element</param>
public XmlHandler(string pXmlFile, bool pFileAutoGenerated, string pRootElement, string pSubRootElement, params string[] pFieldNames)
{
try
{
_FileAutoGenerated = pFileAutoGenerated;
this.ConfigureXmlHandler(pXmlFile, pRootElement, pSubRootElement, pFieldNames);
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// To initialize the xml file with its root element, sub-root element and fields
/// </summary>
/// <param name="pXmlFile">Xml file to be handled (Name with full path)</param>
/// <param name="pFileAutoGenerated">If it is true, then the given xml file will be created if the file does not exist</param>
/// <param name="pRootElement">Root element of the xml file</param>
/// <param name="pSubRootElement">Sub-Root element under the root element of the xml file</param>
/// <param name="pFieldNames">Child tags' name under the sub-root element</param>
/// <param name="pFieldTypes">Child tags' data types</param>
public XmlHandler(string pXmlFile, bool pFileAutoGenerated, string pRootElement, string pSubRootElement, string[] pFieldNames, Type[] pFieldTypes)
{
try
{
_FileAutoGenerated = pFileAutoGenerated;
this.ConfigureXmlHandler(pXmlFile, pRootElement, pSubRootElement, pFieldNames);
this.SetDataType(pFieldTypes);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion

#region Public Methods
#region Add
/// <summary>
/// To append new child tags in xml file with the array of values
/// </summary>
/// <param name="pValues">Array of values</param>
public void Add(params object[] pValues)
{
try
{
DataRow dr;
if (_IsIdentity)
{
int count = 0;
dr = _DSXml.Tables[0].NewRow();
if (pValues.Length >= _XmlFieldNames.Length)
throw new Exception("Invalid number of values");        //Extra Values
for (int i = 0; i < _XmlFieldNames.Length; i++)
{
if (dr.Table.Columns[i].AutoIncrement) continue;
if (pValues.Length == count) throw new Exception("Invalid number of values"); //Less number of values
dr[i] = pValues[count];
count++;
}
_DSXml.Tables[0].Rows.Add(dr);
}
else
{
if (pValues.Length == _XmlFieldNames.Length)
{
dr = _DSXml.Tables[0].NewRow();
dr.ItemArray = pValues;
_DSXml.Tables[0].Rows.Add(dr);
}
else
throw new Exception("Invalid number of values");
}
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// To append child tags with xml file with particular field(tag) values
/// </summary>
/// <param name="pFields">Array of field names (Tag names)</param>
/// <param name="pValues">Corresponding values for array of field names</param>
public void Add(string[] pFields, object[] pValues)
{
try
{
if (pValues.Length != pFields.Length && pFields.Length > 0)
throw new Exception("Number of fields differ from given number of values");
DataRow dr = _DSXml.Tables[0].NewRow();
for (int i = 0; i < pFields.Length; i++) dr[pFields[i].ToString()] = pValues[i];
_DSXml.Tables[0].Rows.Add(dr);
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// To append new child tags in xml file from DATA ROW
/// </summary>
/// <param name="pDR">Data row contains the values for xml file</param>
public void Add(DataRow pDR)
{
try
{
this.Add(pDR.ItemArray);
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// To append new child tags in xml file with the values from DATA TABLE
/// </summary>
/// <param name="pDT">Data table contains the values for xml file</param>
public void Add(DataTable pDT)
{
try
{
if (pDT == null) throw new Exception("DataTable parameter can not be null");
for (int i = 0; i < pDT.Rows.Count; i++) this.Add(pDT.Rows[i].ItemArray);
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// To append new child tags in xml file with the values from DATA SET
/// </summary>
/// <param name="pDS">Data set contains the values for xml file</param>
public void Add(DataSet pDS)
{
try
{
if (pDS == null) throw new Exception("Dataset parameter can not be null");
if(pDS.Tables.Count>0) this.Add(pDS.Tables[0]);
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// To append a xml node
/// </summary>
/// <param name="pXN"></param>
public void Add(XmlNode pXN)
{
try
{
this.Add(pXN.OuterXml);
}
catch (Exception ex)
{
throw ex;
}
}



/// <summary>
/// To append the xml string in xml file
/// </summary>
/// <param name="XmlString">valid xml string</param>
public void AddXml(string pXml)
{
try
{
if (IsValidXml(pXml)) this.Add(ConvertXMLToDataSet(pXml).Tables[0]);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion

#region Update
/// <summary>
/// This method updates the array of values into specified index of xml file
/// </summary>
/// <param name="pIndex">Index of the xml file data</param>
/// <param name="pValues">Array of Values</param>
public void Update(int pIndex, params object[] pValues)
{
try
{
if (pValues.Length != _XmlFieldNames.Length)
throw new Exception("Invalid number of values");
if (_DSXml.Tables.Count > 0)
{
if (_DSXml.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < _XmlFieldNames.Length; i++)
_DSXml.Tables[0].Rows[pIndex][i] = pValues[i];
}
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// This method updates the array of specified field(Tag) values into specified index of xml file
/// </summary>
/// <param name="pIndex">Index</param>
/// <param name="pFields">Field Name (Tag Names)</param>
/// <param name="pValues">Corresponding field values</param>
public void Update(int pIndex, string[] pFields, object[] pValues)
{
try
{
if (pFields.Length != pValues.Length)
throw new Exception("Number of fields differ from given number of values");
if(_DSXml.Tables.Count>0)
{
if(_DSXml.Tables[0].Rows.Count>0)
{
for(int i=0;i<pFields.Length;i++)
_DSXml.Tables[0].Rows[pIndex][pFields[i].ToString()] = pValues[i];
}
}
}
catch(Exception ex)
{
throw ex;
}
}
/// <summary>
/// This method updates an xml node into specified index of xml file
/// </summary>
/// <param name="pIndex">Index</param>
/// <param name="pXN">Valid Xml Node</param>
public void Update(int pIndex, XmlNode pXN)
{
try
{
this.Update(pIndex, pXN.OuterXml);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// This method updates DATA ROW values into specified index of xml file
/// </summary>
/// <param name="pIndex"></param>
/// <param name="pDR"></param>
public void Update(int pIndex, DataRow pDR)
{
try
{
this.Update(pIndex, pDR.ItemArray);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// This method updates the array of specified field(Tag) values in every records which
/// statisfy the specified condition
/// </summary>
/// <param name="pCondition">Valid conditional expression(SQL Standard)</param>
/// <param name="pFields">Array of Field Names (Tag Names)</param>
/// <param name="pValues">Corresponding values for the fields</param>
public void Update(string pCondition, string[] pFields, object[] pValues)
{
try
{
int index;
if (pFields.Length != pValues.Length)
throw new Exception("Number of field names differ from given number of values");
if(_DSXml.Tables.Count>0)
{
if(_DSXml.Tables[0].Rows.Count>0)
{
DataTable dt = _DSXml.Tables[0].Copy();
dt.Columns.Add("XmlHandlerIndexColumn");
for (int i = 0; i < dt.Rows.Count; i++) dt.Rows[i]["XmlHandlerIndexColumn"] = i;
DataView dv=new DataView(dt,pCondition,"",DataViewRowState.CurrentRows);
for(int i=0;i<dv.Count;i++)
{
index = Convert.ToInt32(dv[i]["XmlHandlerIndexColumn"]);
this.Update(index, pFields, pValues);
}
}
}
}
catch(Exception ex)
{
throw ex;
}
}
/// <summary>
/// This method updates specified field(Tag) values in every records which
/// statisfy the specified condition
/// </summary>
/// <param name="pCondition">Valid conditional expression(SQL Standard)</param>
/// <param name="pFields">Field Name (Tag Name)</param>
/// <param name="pValues">Field Value (Tag Value)</param>
public void Update(string pCondition, string pFieldName, object pValue)
{
try
{
this.Update(pCondition, new string[] { pFieldName }, new object[] { pValue });
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// This method updates the xml string into specified index of xml file
/// </summary>
/// <param name="pIndex">Index of the xml file data</param>
/// <param name="pXmlString">Valid xml string</param>
public void UpdateXml(int pIndex, string pXml)
{
try
{
if (IsValidXml(pXml)) this.Update(pIndex, ConvertXMLToDataSet(pXml).Tables[0].Rows[0].ItemArray);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion

#region Delete
/// <summary>
/// Remove the row of xml data at the specified index
/// </summary>
/// <param name="pIndex">Index of xml data to be deleted</param>
public void Delete(int pIndex)
{
try
{
if (_DSXml.Tables.Count > 0)
{
if (pIndex < 0 || pIndex >= _DSXml.Tables[0].Rows.Count) return;
_DSXml.Tables[0].Rows.RemoveAt(pIndex);
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// Remove the rows of xml data for the specified condition
/// </summary>
/// <param name="pCondition">Valid Conditional Expression(SQL Standard)</param>
public void Delete(string pCondition)
{
try
{
if(_DSXml.Tables.Count>0)
{
DataTable dt = _DSXml.Tables[0].Copy();
dt.Columns.Add("XmlHandlerIndexColumn", typeof(int));
for (int i = 0; i < dt.Rows.Count; i++) dt.Rows[i]["XmlHandlerIndexColumn"] = i;
DataView dv=new DataView(dt,pCondition,"",DataViewRowState.CurrentRows);
if(dv.Count>0)
{
for (int i = 0; i < dv.Count; i++)
this.Delete(Convert.ToInt32(dv[i]["XmlHandlerIndexColumn"]) - i);
}
}
}
catch(Exception ex)
{
throw ex;
}
}

/// <summary>
/// Remove the row of xml data at the specified index
/// </summary>
/// <param name="pIndex">Index of xml data to be deleted</param>
public void DeleteAll()
{
try
{
if (_DSXml.Tables.Count > 0)
{
_DSXml.Tables[0].Rows.Clear();
}
}
catch (Exception ex)
{
throw ex;
}
}
#endregion

#region Select
/// <summary>
/// select all data from xml file
/// </summary>
/// <returns></returns>
public DataSet Select()
{
try
{
return _DSXml;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// selecting N-number of records from the specified start index
/// </summary>
/// <param name="pStartIndex">start index</param>
/// <param name="pNoOfRecords">number of records to be selected</param>
/// <returns></returns>
public DataSet Select(int pStartIndex, int pNoOfRecords)
{
try
{
DataSet D = new DataSet();
D.Tables.Add(this.SelectAsDataTable(pStartIndex, pNoOfRecords));
return D;
}
catch(Exception ex)
{
throw ex;
}
}
/// <summary>
/// select particular data which are statisfying certain conditions
/// </summary>
/// <param name="pFilter">Valid conditional expression (SQL Standard)</param>
/// <returns></returns>
public DataSet Select(string pFilter)
{
try
{
DataSet D = new DataSet();
D.Tables.Add(this.SelectAsDataTable(pFilter));
return D;
}
catch(Exception ex)
{
throw ex;
}
}


/// <summary>
/// select all data from xml file
/// </summary>
/// <returns></returns>
public DataSet SelectAsDataset()
{
return this.Select();
}
/// <summary>
/// selecting N-number of records from the specified start index
/// </summary>
/// <param name="pStartIndex">start index</param>
/// <param name="pNoOfRecords">number of records to be selected</param>
/// <returns></returns>
public DataSet SelectAsDataset(int pStartIndex, int pNoOfRecords)
{
return this.Select(pStartIndex, pNoOfRecords);
}
/// <summary>
/// select particular data which are statisfying certain conditions
/// </summary>
/// <param name="pFilter">Valid conditional expression (SQL Standard)</param>
/// <returns></returns>
public DataSet SelectAsDataset(string pFilter)
{
return this.Select(pFilter);
}


/// <summary>
/// select all data from xml file
/// </summary>
/// <returns></returns>
public DataTable SelectAsDataTable()
{
try
{
if (_DSXml.Tables.Count > 0)
{
return _DSXml.Tables[0];
}
else
{
DataTable dt = new DataTable(_SubRootElement);
for (int i = 0; i < _XmlFieldNames.Length; i++)
{
if (_XmlFieldTypes[i] != null)
dt.Columns.Add(_XmlFieldNames.GetValue(i).ToString(),_XmlFieldTypes[i]);
else
dt.Columns.Add(_XmlFieldNames.GetValue(i).ToString());
}
return dt;
}
}
catch(Exception ex)
{
throw ex;
}
}
/// <summary>
/// selecting N-number of records from the specified start index
/// </summary>
/// <param name="pStartIndex">start index</param>
/// <param name="pNoOfRecords">number of records to be selected</param>
/// <returns></returns>
public DataTable SelectAsDataTable(int pStartIndex, int pNoOfRecords)
{
try
{
if (pStartIndex < 0)
throw new Exception("Invalid start index");
if (pNoOfRecords < 0)
throw new Exception("Invalid no of records selection");
if (_DSXml.Tables.Count > 0)
{
DataTable dt = _DSXml.Tables[0].Copy();
dt.Clear();
for (int i = pStartIndex; i < (pStartIndex + pNoOfRecords); i++)
{
if (i >= _DSXml.Tables[0].Rows.Count) break;
dt.Rows.Add(_DSXml.Tables[0].Rows[i].ItemArray);
}
dt.AcceptChanges();
return dt;
}
else
{
return this.SelectAsDataTable();
}
}
catch(Exception ex)
{
throw ex;
}
}
/// <summary>
/// select particular data which are statisfying certain conditions
/// </summary>
/// <param name="pFilter">Valid conditional expression (SQL Standard)</param>
/// <returns></returns>
public DataTable SelectAsDataTable(string pFilter)
{
try
{
if (_DSXml.Tables.Count > 0)
{
DataView dv = new DataView(_DSXml.Tables[0], pFilter, "", DataViewRowState.CurrentRows);
return dv.ToTable();
}
else
{
return this.SelectAsDataTable();
}
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// select all xml value as string
/// </summary>
/// <returns></returns>
public string SelectAsXml()
{
try
{
DataSet ds = this.SelectAsDataset();
return ds.GetXml();
}
catch (Exception ex)
{
throw ex;
}
}
public string SelectAsXml(int pStartIndex, int pNoOfRecords)
{
try
{
DataSet ds = this.SelectAsDataset(pStartIndex, pNoOfRecords);
return ds.GetXml();
}
catch (Exception ex)
{
throw ex;
}
}
public string SelectAsXml(string pFilter)
{
try
{
DataSet ds = this.SelectAsDataset(pFilter);
return ds.GetXml();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion

#region Find
/// <summary>
/// This method returns the index of the xml file data which statisfies the specified condition
/// </summary>
/// <param name="pCondition">Valid conditional expression (SQL Standard)</param>
/// <returns>Index</returns>
public int Find(string pCondition)
{
try
{
int index=-1;
if(_DSXml.Tables.Count>0)
{
DataTable dt = _DSXml.Tables[0].Copy();
dt.Columns.Add("XmlHandlerIndexColumn",typeof(int));
for (int i = 0; i < dt.Rows.Count; i++) dt.Rows[i]["XmlHandlerIndexColumn"] = i;
dt.AcceptChanges();
DataView dv=new DataView(dt,pCondition,"",DataViewRowState.CurrentRows);
if (dv.Count > 0) index = Convert.ToInt32(dv[0]["XmlHandlerIndexColumn"]);
}
return index;
}
catch(Exception ex)
{
throw ex;
}
}
#endregion

#region Insert
/// <summary>
/// Insert the specified set of values into the xml file at the specified index
/// </summary>
/// <param name="pIndex">Index where the values to be inserted</param>
/// <param name="pValues">Set of values</param>
public void Insert(int pIndex,params object[] pValues)
{
DataRow dr;
if (_IsIdentity)
{
int count = 0;
dr = _DSXml.Tables[0].NewRow();
if (pValues.Length >= _XmlFieldNames.Length)
throw new Exception("Invalid number of values");        //Extra Values
for (int i = 0; i < _XmlFieldNames.Length; i++)
{
if (dr.Table.Columns[i].AutoIncrement) continue;
if (pValues.Length == count) throw new Exception("Invalid number of values"); //Less number of values
dr[i] = pValues[count];
count++;
}
_DSXml.Tables[0].Rows.InsertAt(dr,pIndex);
}
else
{
if (pValues.Length == _XmlFieldNames.Length)
{
dr = _DSXml.Tables[0].NewRow();
dr.ItemArray = pValues;
_DSXml.Tables[0].Rows.InsertAt(dr, pIndex);
}
else
throw new Exception("Invalid number of values");
}
}

/// <summary>
/// Insert the specified xml node into the xml file at the specified index
/// </summary>
/// <param name="pIndex">Index where the values to be inserted</param>
/// <param name="pXN">Valid xml node</param>
public void Insert(int pIndex, XmlNode pXN)
{
try
{
this.Insert(pIndex, pXN.OuterXml);
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// Insert the specified data row into the xml file at the specified index
/// </summary>
/// <param name="pIndex">Index where the values to be inserted</param>
/// <param name="pDR">DataRow</param>
public void Insert(int pIndex, DataRow pDR)
{
try
{
this.Insert(pIndex, pDR.ItemArray);
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// Insert the specified xml data into the xml file at the specified index
/// </summary>
/// <param name="pIndex">Index where the values to be inserted</param>
/// <param name="pXml">Valid xml string</param>
public void InsertXML(int pIndex, string pXml)
{
try
{
if (IsValidXml(pXml)) this.Insert(pIndex, ConvertXMLToDataSet(pXml).Tables[0].Rows[0]);
}
catch (Exception ex)
{
throw ex;
}
}

#endregion

#region Set & Remove
/// <summary>
/// Setting the key field
/// </summary>
/// <param name="pFieldName"></param>
public void SetKeyField(string pFieldName)
{
try
{
DataColumn[] dtColumns = new DataColumn[1];
dtColumns[0] = _DSXml.Tables[0].Columns[pFieldName];
_DSXml.Tables[0].PrimaryKey = dtColumns;
_XmlKeyField = pFieldName;
_IsKeyField = true;
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// Remove the key field
/// </summary>
public void RemoveKeyField()
{
_DSXml.Tables[0].PrimaryKey = null;
_XmlKeyField = "";
_IsKeyField = false;
}

/// <summary>
/// Set the auto increment setting to the specified field
/// </summary>
/// <param name="pFieldName">Field name</param>
public void SetAutoIncrement(string pFieldName)
{
try
{
int maxValue = 0,value=0;
_DSXml.Tables[0].Columns[pFieldName].AutoIncrement = true;
if(_DSXml.Tables[0].Rows.Count>0)
maxValue = Convert.ToInt32(_DSXml.Tables[0].Rows[0][pFieldName].ToString());
for (int i = 1; i < _DSXml.Tables[0].Rows.Count; i++)
{
value = Convert.ToInt32(_DSXml.Tables[0].Rows[i][pFieldName].ToString());
if (maxValue < value) maxValue = value;
}
_DSXml.Tables[0].Columns[pFieldName].AutoIncrementSeed = maxValue+1;
_DSXml.Tables[0].Columns[pFieldName].AutoIncrementStep = 1;
_IsIdentity = true;
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// Set the auto increment setting to the specified field
/// </summary>
/// <param name="pFieldName">Field name</param>
/// <param name="pIncrementSeed">Increment Seed</param>
/// <param name="pIncrementStep">Increment Step</param>
public void SetAutoIncrement(string pFieldName, int pIncrementSeed, int pIncrementStep)
{
try
{
_DSXml.Tables[0].Columns[pFieldName].AutoIncrement = true;
_DSXml.Tables[0].Columns[pFieldName].AutoIncrementSeed = pIncrementSeed;
_DSXml.Tables[0].Columns[pFieldName].AutoIncrementStep = pIncrementStep;
_IsIdentity = true;
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// Remove auto increment setting for the specified field
/// </summary>
/// <param name="pFieldName">Field name</param>
public void RemoveAutoIncrement(string pFieldName)
{
int i = 0;
_DSXml.Tables[0].Columns[pFieldName].AutoIncrement = false;
for (i = 0; i < _DSXml.Tables[0].Columns.Count; i++)
{
if (_DSXml.Tables[0].Columns[i].AutoIncrement)
{
_IsIdentity = true;
break;
}
}
if (i >= _DSXml.Tables[0].Columns.Count) _IsIdentity = false;
}

/// <summary>
/// Remove auto increment setting for all fields
/// </summary>
public void RemoveAutoIncrement()
{
for (int i = 0; i < _DSXml.Tables[0].Columns.Count; i++) _DSXml.Tables[0].Columns[i].AutoIncrement = false;
_IsIdentity = false;
}
#endregion

#region Transaction
public void Commit()
{
HttpContext.Current.Application.Lock();
_DSXml.WriteXml(_XmlFileName);
HttpContext.Current.Application.UnLock();
}

public void RollBack()
{
_DSXml.ReadXml(_XmlFileName);
}
#endregion
#endregion

#region Public Static Method
public static DataSet ConvertXMLToDataSet(string XmlString)
{
StringReader SR = null;
XmlTextReader XTR = null;
try
{
DataSet DS = new DataSet();
SR = new StringReader(XmlString);
XTR = new XmlTextReader(SR);
DS.ReadXml(XTR);
return DS;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (XTR != null) XTR.Close();
}
}

public static string ConvertRawXMLToShowHTML(string XmlString)
{
return XmlString.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("
","<br/>");
}

public static int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}

public static string RandomString(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
Random random = new Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}

#endregion

#region Private Methods
private bool IsValidFile()
{
try
{
FileInfo fileInfo = new FileInfo(_XmlFileName);
if (fileInfo.Extension.ToLower() != ".xml")
throw new Exception(_XmlFileName + " should be a xml file");
if (!fileInfo.Exists && !_FileAutoGenerated)
throw new Exception(_XmlFileName + " does not exist");
if (!fileInfo.Exists)
{
_XmlDoc = new XmlDocument();
_XmlDoc.LoadXml("<" + RootElement + "></" + RootElement + ">");
_XmlDoc.Save(_XmlFileName);
}
_DSXml = new DataSet();
_DSXml.ReadXml(_XmlFileName);
if (_DSXml.Tables.Count > 1)
throw new Exception(_XmlFileName + " is not in a valid format");
else if (_DSXml.Tables.Count > 0)
{
if (_DSXml.DataSetName != _RootElement)
throw new Exception("Invalid root element");
if (_DSXml.Tables[0].TableName != _SubRootElement)
throw new Exception("Invalid sub root element");
if (_DSXml.Tables[0].Columns.Count != _XmlFieldNames.Length)
throw new Exception("Invalid number of fields");
for (int i = 0; i < _DSXml.Tables[0].Columns.Count; i++)
{
if (_DSXml.Tables[0].Columns[i].ColumnName != _XmlFieldNames.GetValue(i).ToString())
throw new Exception("Invalid field names");
}
}
else
{
if (_DSXml.DataSetName != _RootElement)
throw new Exception("Invalid root element");
DataTable dt = new DataTable(_SubRootElement);
for (int i = 0; i < _XmlFieldNames.Length; i++) dt.Columns.Add(_XmlFieldNames.GetValue(i).ToString());
_DSXml.Tables.Add(dt);
}
return true;
}
catch (Exception ex)
{
throw ex;
}
}
private bool IsValidXml(string pXml)
{
try
{
DataSet D=new DataSet();
D = ConvertXMLToDataSet(pXml);
if(D.Tables.Count!=1) throw new Exception("Invalid xml");
if (_IsIdentity)
{
int count = 0;
if (D.Tables[0].Columns.Count > _XmlFieldNames.Length)
throw new Exception("Invalid number of xml fields");
for (int i = 0; i < _XmlFieldNames.Length; i++)
{
if (_DSXml.Tables[0].Columns[i].AutoIncrement) continue;
if (count == D.Tables[0].Columns.Count) throw new Exception("Invalid number of fields");
if (D.Tables[0].Columns[count].ColumnName != _XmlFieldNames.GetValue(i).ToString())
throw new Exception("Invalid xml fields or the fields may be in disorder");
count++;
}
}
else
{
if (D.Tables[0].Columns.Count != _XmlFieldNames.Length)
throw new Exception("Invalid number of xml fields");
for (int i = 0; i < _XmlFieldNames.Length; i++)
{
if (D.Tables[0].Columns[i].ColumnName != _XmlFieldNames.GetValue(i).ToString())
throw new Exception("Invalid xml fields or the fields may be in disorder");
}
}
return true;
}
catch(Exception ex)
{
throw ex;
}
}
private void ConfigureXmlHandler(string pXmlFile, string pRootElement, string pSubRootElement, params string[] pFieldNames)
{
_RootElement = pRootElement;
_SubRootElement = pSubRootElement;
_XmlFieldNames = pFieldNames;
_FieldCount = _XmlFieldNames.Length;
_XmlFileName = pXmlFile;
_XmlKeyField = "";
_IsIdentity = false;
_IsKeyField = false;
if (_XmlFieldNames == null) throw new Exception("Xml file can not be without fields");
if (_XmlFieldNames.Length == 0) throw new Exception("Xml file can not be without fields");
if (IsValidFile())
{
if (_DSXml.Tables.Count > 0)
_RecordCount = _DSXml.Tables[0].Rows.Count;
else
_RecordCount = 0;
}
}
public void SetDataType(params Type[] pTypes)
{
try
{
string keyField = "";
if (_XmlFieldNames.Length == pTypes.Length)
{
DataSet ds = new DataSet(_RootElement);
DataTable dt = new DataTable(_SubRootElement);
for (int i = 0; i < _XmlFieldNames.Length; i++) dt.Columns.Add(_XmlFieldNames[i], pTypes[i]);
ds.Tables.Add(dt);
for (int i = 0; i < _DSXml.Tables[0].Rows.Count; i++) ds.Tables[0].Rows.Add(_DSXml.Tables[0].Rows[i].ItemArray);
for (int i = 0; i < _XmlFieldNames.Length; i++)
{
ds.Tables[0].Columns[i].AutoIncrement = _DSXml.Tables[0].Columns[i].AutoIncrement;
ds.Tables[0].Columns[i].AutoIncrementSeed = _DSXml.Tables[0].Columns[i].AutoIncrementSeed;
ds.Tables[0].Columns[i].AutoIncrementStep = _DSXml.Tables[0].Columns[i].AutoIncrementStep;
}
if (_IsKeyField)
{
DataColumn[] dtColumns = _DSXml.Tables[0].PrimaryKey;
DataColumn dtColumn = (DataColumn)dtColumns.GetValue(0);
keyField = dtColumn.ColumnName;
}
_DSXml = ds;
if (keyField != "") this.SetKeyField(keyField);
_RecordCount = _DSXml.Tables[0].Rows.Count;
_XmlFieldTypes = pTypes;
}
else
{
throw new Exception("No of columns differ from given data types");
}
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
}

source