/ Published in: C#
URL: ideabubbling.com
Everything is either true or untrue, or both true and untrue, or neither true nor untrue
Expand |
Embed | Plain Text
/** * XmlHandler.NET * Version: Beta 1.0 * Released: 10 Dec, 2007 * Core Developer:Jegatheeswaran([email protected]) * Credits:Vasanthakumar([email protected]), Syed Vaisul Karne([email protected]) * 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:<Students><Student><Name/><Age/></Student></Students></param> public XmlHandler(string pXmlFile, bool pFileAutoGenerated, string pXmlStructure) { try { D = ConvertXMLToDataSet(pXmlStructure); if(D.Tables.Count!=1) 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) 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 } } 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) 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 { 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.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) 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) 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) 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; 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 { } 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(); for (int i = 0; i < dt.Rows.Count; i++) dt.Rows[i]["XmlHandlerIndexColumn"] = i; 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 { 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 { 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 { 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) if (pNoOfRecords < 0) 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) { 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(); for (int i = 0; i < dt.Rows.Count; i++) dt.Rows[i]["XmlHandlerIndexColumn"] = i; dt.AcceptChanges(); 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) 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 } } /// <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 { 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 { 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("&", "&").Replace("<", "<").Replace(">", ">").Replace("\n","<br/>"); } public static int RandomNumber(int min, int max) { return random.Next(min, max); } public static string RandomString(int size, bool lowerCase) { 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 { if (fileInfo.Extension.ToLower() != ".xml") if (!fileInfo.Exists && !_FileAutoGenerated) if (!fileInfo.Exists) { _XmlDoc.LoadXml("<" + RootElement + "></" + RootElement + ">"); _XmlDoc.Save(_XmlFileName); } _DSXml.ReadXml(_XmlFileName); if (_DSXml.Tables.Count > 1) else if (_DSXml.Tables.Count > 0) { if (_DSXml.DataSetName != _RootElement) if (_DSXml.Tables[0].TableName != _SubRootElement) if (_DSXml.Tables[0].Columns.Count != _XmlFieldNames.Length) for (int i = 0; i < _DSXml.Tables[0].Columns.Count; i++) { if (_DSXml.Tables[0].Columns[i].ColumnName != _XmlFieldNames.GetValue(i).ToString()) } } else { if (_DSXml.DataSetName != _RootElement) 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 { D = ConvertXMLToDataSet(pXml); if (_IsIdentity) { int count = 0; if (D.Tables[0].Columns.Count > _XmlFieldNames.Length) for (int i = 0; i < _XmlFieldNames.Length; i++) { if (_DSXml.Tables[0].Columns[i].AutoIncrement) continue; if (D.Tables[0].Columns[count].ColumnName != _XmlFieldNames.GetValue(i).ToString()) count++; } } else { if (D.Tables[0].Columns.Count != _XmlFieldNames.Length) for (int i = 0; i < _XmlFieldNames.Length; i++) { if (D.Tables[0].Columns[i].ColumnName != _XmlFieldNames.GetValue(i).ToString()) } } 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 (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) { 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 { } } catch (Exception ex) { throw ex; } } #endregion } }
You need to login to post a comment.
