Posted By

zvasanth on 12/29/07


Tagged

database data xml handler store datastore


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

umang_nine


xml handler


 / Published in: C#
 

URL: ideabubbling.com

Everything is either true or untrue, or both true and untrue, or neither true nor untrue

  1. /**
  2.   * XmlHandler.NET
  3.   * Version: Beta 1.0
  4.   * Released: 10 Dec, 2007
  5.   * Core Developer:Jegatheeswaran([email protected])
  6.   * Credits:Vasanthakumar([email protected]), Syed Vaisul Karne([email protected])
  7.   * Copyright (C) 2007
  8.  
  9.   * This program is free software; you can redistribute it and/or
  10.   * modify it under the terms of the GNU Lesser General Public License
  11.   * as published by the Free Software Foundation; either version 2
  12.   * of the License, or (at your option) any later version.
  13.  
  14.   * This program is distributed in the hope that it will be useful,
  15.   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17.   * GNU Lesser General Public License for more details.
  18.  
  19.   * You should have received a copy of the GNU Lesser General Public License
  20.   * along with this program; if not, write to the Free Software
  21.   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22.   **/
  23.  
  24. using System;
  25. using System.Collections.Generic;
  26. using System.Text;
  27. using System.IO;
  28. using System.Xml;
  29. using System.Data;
  30. using System.Web;
  31.  
  32. namespace ideaBubbling.Xml
  33. {
  34. /// <summary>
  35. /// A table type xml file can be handled easily through this XmlHandler class
  36. /// </summary>
  37. public class XmlHandler
  38. {
  39. #region Private Memebers
  40. private string _RootElement;
  41. private string _SubRootElement;
  42. private string _XmlFileName;
  43. private string[] _XmlFieldNames;
  44. private Type[] _XmlFieldTypes;
  45. private string _XmlKeyField;
  46. private DataSet _DSXml;
  47. private XmlDocument _XmlDoc;
  48. private int _RecordCount;
  49. private int _FieldCount;
  50. private bool _FileAutoGenerated;
  51. bool _IsIdentity;
  52. bool _IsKeyField;
  53. #endregion
  54.  
  55. #region Properties
  56. public string File
  57. {
  58. get
  59. {
  60. return _XmlFileName;
  61. }
  62. set
  63. {
  64. this.Commit();
  65. _XmlFileName = value;
  66. if (IsValidFile())
  67. {
  68. if (_DSXml.Tables.Count > 0)
  69. _RecordCount = _DSXml.Tables[0].Rows.Count;
  70. else
  71. _RecordCount = 0;
  72. }
  73. }
  74. }
  75. public string RootElement
  76. {
  77. get
  78. {
  79. return _RootElement;
  80. }
  81. }
  82. public string SubRootElement
  83. {
  84. get
  85. {
  86. return _SubRootElement;
  87. }
  88. }
  89. public string[] FieldNames
  90. {
  91. get
  92. {
  93. return _XmlFieldNames;
  94. }
  95. }
  96. //Count is renamed as RecordCount by Syed.
  97. public int RecordCount
  98. {
  99. get
  100. {
  101. return _RecordCount;
  102. }
  103. }
  104. #endregion
  105.  
  106. #region Indexers
  107. public DataRow this[int pIndex]
  108. {
  109. get
  110. {
  111. return _DSXml.Tables[0].Rows[pIndex];
  112. }
  113. }
  114. #endregion
  115.  
  116. #region Constructors
  117. private XmlHandler(){}
  118.  
  119. /// <summary>
  120. /// To initialize the xml file with its structure
  121. /// </summary>
  122. /// <param name="pXmlFile">Xml file to be handled (Name with full path)</param>
  123. /// <param name="pFileAutoGenerated">If it is true, then the given xml file will be created if the file does not exist</param>
  124. /// <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>
  125. public XmlHandler(string pXmlFile, bool pFileAutoGenerated, string pXmlStructure)
  126. {
  127. try
  128. {
  129. DataSet D = new DataSet();
  130. D = ConvertXMLToDataSet(pXmlStructure);
  131. if(D.Tables.Count!=1)
  132. throw new Exception("Invalid xml structure");
  133. string[] fieldNames=new string[D.Tables[0].Columns.Count];
  134. for(int i=0;i<D.Tables[0].Columns.Count;i++)
  135. fieldNames.SetValue(D.Tables[0].Columns[i].ColumnName, i);
  136. _FileAutoGenerated = pFileAutoGenerated;
  137. this.ConfigureXmlHandler(pXmlFile, D.DataSetName, D.Tables[0].TableName, fieldNames);
  138. }
  139. catch (Exception ex)
  140. {
  141. throw ex;
  142. }
  143. }
  144.  
  145. /// <summary>
  146. /// To initialize the xml file with its root element, sub-root element and fields
  147. /// </summary>
  148. /// <param name="pXmlFile">Xml file to be handled (Name with full path)</param>
  149. /// <param name="pFileAutoGenerated">If it is true, then the given xml file will be created if the file does not exist</param>
  150. /// <param name="pRootElement">Root element of the xml file</param>
  151. /// <param name="pSubRootElement">Sub-Root element under the root element of the xml file</param>
  152. /// <param name="pFieldNames">Child tags' name under the sub-root element</param>
  153. public XmlHandler(string pXmlFile, bool pFileAutoGenerated, string pRootElement, string pSubRootElement, params string[] pFieldNames)
  154. {
  155. try
  156. {
  157. _FileAutoGenerated = pFileAutoGenerated;
  158. this.ConfigureXmlHandler(pXmlFile, pRootElement, pSubRootElement, pFieldNames);
  159. }
  160. catch (Exception ex)
  161. {
  162. throw ex;
  163. }
  164. }
  165.  
  166. /// <summary>
  167. /// To initialize the xml file with its root element, sub-root element and fields
  168. /// </summary>
  169. /// <param name="pXmlFile">Xml file to be handled (Name with full path)</param>
  170. /// <param name="pFileAutoGenerated">If it is true, then the given xml file will be created if the file does not exist</param>
  171. /// <param name="pRootElement">Root element of the xml file</param>
  172. /// <param name="pSubRootElement">Sub-Root element under the root element of the xml file</param>
  173. /// <param name="pFieldNames">Child tags' name under the sub-root element</param>
  174. /// <param name="pFieldTypes">Child tags' data types</param>
  175. public XmlHandler(string pXmlFile, bool pFileAutoGenerated, string pRootElement, string pSubRootElement, string[] pFieldNames, Type[] pFieldTypes)
  176. {
  177. try
  178. {
  179. _FileAutoGenerated = pFileAutoGenerated;
  180. this.ConfigureXmlHandler(pXmlFile, pRootElement, pSubRootElement, pFieldNames);
  181. this.SetDataType(pFieldTypes);
  182. }
  183. catch (Exception ex)
  184. {
  185. throw ex;
  186. }
  187. }
  188. #endregion
  189.  
  190. #region Public Methods
  191. #region Add
  192. /// <summary>
  193. /// To append new child tags in xml file with the array of values
  194. /// </summary>
  195. /// <param name="pValues">Array of values</param>
  196. public void Add(params object[] pValues)
  197. {
  198. try
  199. {
  200. DataRow dr;
  201. if (_IsIdentity)
  202. {
  203. int count = 0;
  204. dr = _DSXml.Tables[0].NewRow();
  205. if (pValues.Length >= _XmlFieldNames.Length)
  206. throw new Exception("Invalid number of values"); //Extra Values
  207. for (int i = 0; i < _XmlFieldNames.Length; i++)
  208. {
  209. if (dr.Table.Columns[i].AutoIncrement) continue;
  210. if (pValues.Length == count) throw new Exception("Invalid number of values"); //Less number of values
  211. dr[i] = pValues[count];
  212. count++;
  213. }
  214. _DSXml.Tables[0].Rows.Add(dr);
  215. }
  216. else
  217. {
  218. if (pValues.Length == _XmlFieldNames.Length)
  219. {
  220. dr = _DSXml.Tables[0].NewRow();
  221. dr.ItemArray = pValues;
  222. _DSXml.Tables[0].Rows.Add(dr);
  223. }
  224. else
  225. throw new Exception("Invalid number of values");
  226. }
  227. }
  228. catch (Exception ex)
  229. {
  230. throw ex;
  231. }
  232. }
  233.  
  234. /// <summary>
  235. /// To append child tags with xml file with particular field(tag) values
  236. /// </summary>
  237. /// <param name="pFields">Array of field names (Tag names)</param>
  238. /// <param name="pValues">Corresponding values for array of field names</param>
  239. public void Add(string[] pFields, object[] pValues)
  240. {
  241. try
  242. {
  243. if (pValues.Length != pFields.Length && pFields.Length > 0)
  244. throw new Exception("Number of fields differ from given number of values");
  245. DataRow dr = _DSXml.Tables[0].NewRow();
  246. for (int i = 0; i < pFields.Length; i++) dr[pFields[i].ToString()] = pValues[i];
  247. _DSXml.Tables[0].Rows.Add(dr);
  248. }
  249. catch (Exception ex)
  250. {
  251. throw ex;
  252. }
  253. }
  254.  
  255. /// <summary>
  256. /// To append new child tags in xml file from DATA ROW
  257. /// </summary>
  258. /// <param name="pDR">Data row contains the values for xml file</param>
  259. public void Add(DataRow pDR)
  260. {
  261. try
  262. {
  263. this.Add(pDR.ItemArray);
  264. }
  265. catch (Exception ex)
  266. {
  267. throw ex;
  268. }
  269. }
  270.  
  271. /// <summary>
  272. /// To append new child tags in xml file with the values from DATA TABLE
  273. /// </summary>
  274. /// <param name="pDT">Data table contains the values for xml file</param>
  275. public void Add(DataTable pDT)
  276. {
  277. try
  278. {
  279. if (pDT == null) throw new Exception("DataTable parameter can not be null");
  280. for (int i = 0; i < pDT.Rows.Count; i++) this.Add(pDT.Rows[i].ItemArray);
  281. }
  282. catch (Exception ex)
  283. {
  284. throw ex;
  285. }
  286. }
  287.  
  288. /// <summary>
  289. /// To append new child tags in xml file with the values from DATA SET
  290. /// </summary>
  291. /// <param name="pDS">Data set contains the values for xml file</param>
  292. public void Add(DataSet pDS)
  293. {
  294. try
  295. {
  296. if (pDS == null) throw new Exception("Dataset parameter can not be null");
  297. if(pDS.Tables.Count>0) this.Add(pDS.Tables[0]);
  298. }
  299. catch (Exception ex)
  300. {
  301. throw ex;
  302. }
  303. }
  304.  
  305. /// <summary>
  306. /// To append a xml node
  307. /// </summary>
  308. /// <param name="pXN"></param>
  309. public void Add(XmlNode pXN)
  310. {
  311. try
  312. {
  313. this.Add(pXN.OuterXml);
  314. }
  315. catch (Exception ex)
  316. {
  317. throw ex;
  318. }
  319. }
  320.  
  321.  
  322.  
  323. /// <summary>
  324. /// To append the xml string in xml file
  325. /// </summary>
  326. /// <param name="XmlString">valid xml string</param>
  327. public void AddXml(string pXml)
  328. {
  329. try
  330. {
  331. if (IsValidXml(pXml)) this.Add(ConvertXMLToDataSet(pXml).Tables[0]);
  332. }
  333. catch (Exception ex)
  334. {
  335. throw ex;
  336. }
  337. }
  338. #endregion
  339.  
  340. #region Update
  341. /// <summary>
  342. /// This method updates the array of values into specified index of xml file
  343. /// </summary>
  344. /// <param name="pIndex">Index of the xml file data</param>
  345. /// <param name="pValues">Array of Values</param>
  346. public void Update(int pIndex, params object[] pValues)
  347. {
  348. try
  349. {
  350. if (pValues.Length != _XmlFieldNames.Length)
  351. throw new Exception("Invalid number of values");
  352. if (_DSXml.Tables.Count > 0)
  353. {
  354. if (_DSXml.Tables[0].Rows.Count > 0)
  355. {
  356. for (int i = 0; i < _XmlFieldNames.Length; i++)
  357. _DSXml.Tables[0].Rows[pIndex][i] = pValues[i];
  358. }
  359. }
  360. }
  361. catch (Exception ex)
  362. {
  363. throw ex;
  364. }
  365. }
  366. /// <summary>
  367. /// This method updates the array of specified field(Tag) values into specified index of xml file
  368. /// </summary>
  369. /// <param name="pIndex">Index</param>
  370. /// <param name="pFields">Field Name (Tag Names)</param>
  371. /// <param name="pValues">Corresponding field values</param>
  372. public void Update(int pIndex, string[] pFields, object[] pValues)
  373. {
  374. try
  375. {
  376. if (pFields.Length != pValues.Length)
  377. throw new Exception("Number of fields differ from given number of values");
  378. if(_DSXml.Tables.Count>0)
  379. {
  380. if(_DSXml.Tables[0].Rows.Count>0)
  381. {
  382. for(int i=0;i<pFields.Length;i++)
  383. _DSXml.Tables[0].Rows[pIndex][pFields[i].ToString()] = pValues[i];
  384. }
  385. }
  386. }
  387. catch(Exception ex)
  388. {
  389. throw ex;
  390. }
  391. }
  392. /// <summary>
  393. /// This method updates an xml node into specified index of xml file
  394. /// </summary>
  395. /// <param name="pIndex">Index</param>
  396. /// <param name="pXN">Valid Xml Node</param>
  397. public void Update(int pIndex, XmlNode pXN)
  398. {
  399. try
  400. {
  401. this.Update(pIndex, pXN.OuterXml);
  402. }
  403. catch (Exception ex)
  404. {
  405. throw ex;
  406. }
  407. }
  408. /// <summary>
  409. /// This method updates DATA ROW values into specified index of xml file
  410. /// </summary>
  411. /// <param name="pIndex"></param>
  412. /// <param name="pDR"></param>
  413. public void Update(int pIndex, DataRow pDR)
  414. {
  415. try
  416. {
  417. this.Update(pIndex, pDR.ItemArray);
  418. }
  419. catch (Exception ex)
  420. {
  421. throw ex;
  422. }
  423. }
  424. /// <summary>
  425. /// This method updates the array of specified field(Tag) values in every records which
  426. /// statisfy the specified condition
  427. /// </summary>
  428. /// <param name="pCondition">Valid conditional expression(SQL Standard)</param>
  429. /// <param name="pFields">Array of Field Names (Tag Names)</param>
  430. /// <param name="pValues">Corresponding values for the fields</param>
  431. public void Update(string pCondition, string[] pFields, object[] pValues)
  432. {
  433. try
  434. {
  435. int index;
  436. if (pFields.Length != pValues.Length)
  437. throw new Exception("Number of field names differ from given number of values");
  438. if(_DSXml.Tables.Count>0)
  439. {
  440. if(_DSXml.Tables[0].Rows.Count>0)
  441. {
  442. DataTable dt = _DSXml.Tables[0].Copy();
  443. dt.Columns.Add("XmlHandlerIndexColumn");
  444. for (int i = 0; i < dt.Rows.Count; i++) dt.Rows[i]["XmlHandlerIndexColumn"] = i;
  445. DataView dv=new DataView(dt,pCondition,"",DataViewRowState.CurrentRows);
  446. for(int i=0;i<dv.Count;i++)
  447. {
  448. index = Convert.ToInt32(dv[i]["XmlHandlerIndexColumn"]);
  449. this.Update(index, pFields, pValues);
  450. }
  451. }
  452. }
  453. }
  454. catch(Exception ex)
  455. {
  456. throw ex;
  457. }
  458. }
  459. /// <summary>
  460. /// This method updates specified field(Tag) values in every records which
  461. /// statisfy the specified condition
  462. /// </summary>
  463. /// <param name="pCondition">Valid conditional expression(SQL Standard)</param>
  464. /// <param name="pFields">Field Name (Tag Name)</param>
  465. /// <param name="pValues">Field Value (Tag Value)</param>
  466. public void Update(string pCondition, string pFieldName, object pValue)
  467. {
  468. try
  469. {
  470. this.Update(pCondition, new string[] { pFieldName }, new object[] { pValue });
  471. }
  472. catch (Exception ex)
  473. {
  474. throw ex;
  475. }
  476. }
  477. /// <summary>
  478. /// This method updates the xml string into specified index of xml file
  479. /// </summary>
  480. /// <param name="pIndex">Index of the xml file data</param>
  481. /// <param name="pXmlString">Valid xml string</param>
  482. public void UpdateXml(int pIndex, string pXml)
  483. {
  484. try
  485. {
  486. if (IsValidXml(pXml)) this.Update(pIndex, ConvertXMLToDataSet(pXml).Tables[0].Rows[0].ItemArray);
  487. }
  488. catch (Exception ex)
  489. {
  490. throw ex;
  491. }
  492. }
  493. #endregion
  494.  
  495. #region Delete
  496. /// <summary>
  497. /// Remove the row of xml data at the specified index
  498. /// </summary>
  499. /// <param name="pIndex">Index of xml data to be deleted</param>
  500. public void Delete(int pIndex)
  501. {
  502. try
  503. {
  504. if (_DSXml.Tables.Count > 0)
  505. {
  506. if (pIndex < 0 || pIndex >= _DSXml.Tables[0].Rows.Count) return;
  507. _DSXml.Tables[0].Rows.RemoveAt(pIndex);
  508. }
  509. }
  510. catch (Exception ex)
  511. {
  512. throw ex;
  513. }
  514. }
  515. /// <summary>
  516. /// Remove the rows of xml data for the specified condition
  517. /// </summary>
  518. /// <param name="pCondition">Valid Conditional Expression(SQL Standard)</param>
  519. public void Delete(string pCondition)
  520. {
  521. try
  522. {
  523. if(_DSXml.Tables.Count>0)
  524. {
  525. DataTable dt = _DSXml.Tables[0].Copy();
  526. dt.Columns.Add("XmlHandlerIndexColumn", typeof(int));
  527. for (int i = 0; i < dt.Rows.Count; i++) dt.Rows[i]["XmlHandlerIndexColumn"] = i;
  528. DataView dv=new DataView(dt,pCondition,"",DataViewRowState.CurrentRows);
  529. if(dv.Count>0)
  530. {
  531. for (int i = 0; i < dv.Count; i++)
  532. this.Delete(Convert.ToInt32(dv[i]["XmlHandlerIndexColumn"]) - i);
  533. }
  534. }
  535. }
  536. catch(Exception ex)
  537. {
  538. throw ex;
  539. }
  540. }
  541.  
  542. /// <summary>
  543. /// Remove the row of xml data at the specified index
  544. /// </summary>
  545. /// <param name="pIndex">Index of xml data to be deleted</param>
  546. public void DeleteAll()
  547. {
  548. try
  549. {
  550. if (_DSXml.Tables.Count > 0)
  551. {
  552. _DSXml.Tables[0].Rows.Clear();
  553. }
  554. }
  555. catch (Exception ex)
  556. {
  557. throw ex;
  558. }
  559. }
  560. #endregion
  561.  
  562. #region Select
  563. /// <summary>
  564. /// select all data from xml file
  565. /// </summary>
  566. /// <returns></returns>
  567. public DataSet Select()
  568. {
  569. try
  570. {
  571. return _DSXml;
  572. }
  573. catch (Exception ex)
  574. {
  575. throw ex;
  576. }
  577. }
  578. /// <summary>
  579. /// selecting N-number of records from the specified start index
  580. /// </summary>
  581. /// <param name="pStartIndex">start index</param>
  582. /// <param name="pNoOfRecords">number of records to be selected</param>
  583. /// <returns></returns>
  584. public DataSet Select(int pStartIndex, int pNoOfRecords)
  585. {
  586. try
  587. {
  588. DataSet D = new DataSet();
  589. D.Tables.Add(this.SelectAsDataTable(pStartIndex, pNoOfRecords));
  590. return D;
  591. }
  592. catch(Exception ex)
  593. {
  594. throw ex;
  595. }
  596. }
  597. /// <summary>
  598. /// select particular data which are statisfying certain conditions
  599. /// </summary>
  600. /// <param name="pFilter">Valid conditional expression (SQL Standard)</param>
  601. /// <returns></returns>
  602. public DataSet Select(string pFilter)
  603. {
  604. try
  605. {
  606. DataSet D = new DataSet();
  607. D.Tables.Add(this.SelectAsDataTable(pFilter));
  608. return D;
  609. }
  610. catch(Exception ex)
  611. {
  612. throw ex;
  613. }
  614. }
  615.  
  616.  
  617. /// <summary>
  618. /// select all data from xml file
  619. /// </summary>
  620. /// <returns></returns>
  621. public DataSet SelectAsDataset()
  622. {
  623. return this.Select();
  624. }
  625. /// <summary>
  626. /// selecting N-number of records from the specified start index
  627. /// </summary>
  628. /// <param name="pStartIndex">start index</param>
  629. /// <param name="pNoOfRecords">number of records to be selected</param>
  630. /// <returns></returns>
  631. public DataSet SelectAsDataset(int pStartIndex, int pNoOfRecords)
  632. {
  633. return this.Select(pStartIndex, pNoOfRecords);
  634. }
  635. /// <summary>
  636. /// select particular data which are statisfying certain conditions
  637. /// </summary>
  638. /// <param name="pFilter">Valid conditional expression (SQL Standard)</param>
  639. /// <returns></returns>
  640. public DataSet SelectAsDataset(string pFilter)
  641. {
  642. return this.Select(pFilter);
  643. }
  644.  
  645.  
  646. /// <summary>
  647. /// select all data from xml file
  648. /// </summary>
  649. /// <returns></returns>
  650. public DataTable SelectAsDataTable()
  651. {
  652. try
  653. {
  654. if (_DSXml.Tables.Count > 0)
  655. {
  656. return _DSXml.Tables[0];
  657. }
  658. else
  659. {
  660. DataTable dt = new DataTable(_SubRootElement);
  661. for (int i = 0; i < _XmlFieldNames.Length; i++)
  662. {
  663. if (_XmlFieldTypes[i] != null)
  664. dt.Columns.Add(_XmlFieldNames.GetValue(i).ToString(),_XmlFieldTypes[i]);
  665. else
  666. dt.Columns.Add(_XmlFieldNames.GetValue(i).ToString());
  667. }
  668. return dt;
  669. }
  670. }
  671. catch(Exception ex)
  672. {
  673. throw ex;
  674. }
  675. }
  676. /// <summary>
  677. /// selecting N-number of records from the specified start index
  678. /// </summary>
  679. /// <param name="pStartIndex">start index</param>
  680. /// <param name="pNoOfRecords">number of records to be selected</param>
  681. /// <returns></returns>
  682. public DataTable SelectAsDataTable(int pStartIndex, int pNoOfRecords)
  683. {
  684. try
  685. {
  686. if (pStartIndex < 0)
  687. throw new Exception("Invalid start index");
  688. if (pNoOfRecords < 0)
  689. throw new Exception("Invalid no of records selection");
  690. if (_DSXml.Tables.Count > 0)
  691. {
  692. DataTable dt = _DSXml.Tables[0].Copy();
  693. dt.Clear();
  694. for (int i = pStartIndex; i < (pStartIndex + pNoOfRecords); i++)
  695. {
  696. if (i >= _DSXml.Tables[0].Rows.Count) break;
  697. dt.Rows.Add(_DSXml.Tables[0].Rows[i].ItemArray);
  698. }
  699. dt.AcceptChanges();
  700. return dt;
  701. }
  702. else
  703. {
  704. return this.SelectAsDataTable();
  705. }
  706. }
  707. catch(Exception ex)
  708. {
  709. throw ex;
  710. }
  711. }
  712. /// <summary>
  713. /// select particular data which are statisfying certain conditions
  714. /// </summary>
  715. /// <param name="pFilter">Valid conditional expression (SQL Standard)</param>
  716. /// <returns></returns>
  717. public DataTable SelectAsDataTable(string pFilter)
  718. {
  719. try
  720. {
  721. if (_DSXml.Tables.Count > 0)
  722. {
  723. DataView dv = new DataView(_DSXml.Tables[0], pFilter, "", DataViewRowState.CurrentRows);
  724. return dv.ToTable();
  725. }
  726. else
  727. {
  728. return this.SelectAsDataTable();
  729. }
  730. }
  731. catch (Exception ex)
  732. {
  733. throw ex;
  734. }
  735. }
  736.  
  737. /// <summary>
  738. /// select all xml value as string
  739. /// </summary>
  740. /// <returns></returns>
  741. public string SelectAsXml()
  742. {
  743. try
  744. {
  745. DataSet ds = this.SelectAsDataset();
  746. return ds.GetXml();
  747. }
  748. catch (Exception ex)
  749. {
  750. throw ex;
  751. }
  752. }
  753. public string SelectAsXml(int pStartIndex, int pNoOfRecords)
  754. {
  755. try
  756. {
  757. DataSet ds = this.SelectAsDataset(pStartIndex, pNoOfRecords);
  758. return ds.GetXml();
  759. }
  760. catch (Exception ex)
  761. {
  762. throw ex;
  763. }
  764. }
  765. public string SelectAsXml(string pFilter)
  766. {
  767. try
  768. {
  769. DataSet ds = this.SelectAsDataset(pFilter);
  770. return ds.GetXml();
  771. }
  772. catch (Exception ex)
  773. {
  774. throw ex;
  775. }
  776. }
  777. #endregion
  778.  
  779. #region Find
  780. /// <summary>
  781. /// This method returns the index of the xml file data which statisfies the specified condition
  782. /// </summary>
  783. /// <param name="pCondition">Valid conditional expression (SQL Standard)</param>
  784. /// <returns>Index</returns>
  785. public int Find(string pCondition)
  786. {
  787. try
  788. {
  789. int index=-1;
  790. if(_DSXml.Tables.Count>0)
  791. {
  792. DataTable dt = _DSXml.Tables[0].Copy();
  793. dt.Columns.Add("XmlHandlerIndexColumn",typeof(int));
  794. for (int i = 0; i < dt.Rows.Count; i++) dt.Rows[i]["XmlHandlerIndexColumn"] = i;
  795. dt.AcceptChanges();
  796. DataView dv=new DataView(dt,pCondition,"",DataViewRowState.CurrentRows);
  797. if (dv.Count > 0) index = Convert.ToInt32(dv[0]["XmlHandlerIndexColumn"]);
  798. }
  799. return index;
  800. }
  801. catch(Exception ex)
  802. {
  803. throw ex;
  804. }
  805. }
  806. #endregion
  807.  
  808. #region Insert
  809. /// <summary>
  810. /// Insert the specified set of values into the xml file at the specified index
  811. /// </summary>
  812. /// <param name="pIndex">Index where the values to be inserted</param>
  813. /// <param name="pValues">Set of values</param>
  814. public void Insert(int pIndex,params object[] pValues)
  815. {
  816. DataRow dr;
  817. if (_IsIdentity)
  818. {
  819. int count = 0;
  820. dr = _DSXml.Tables[0].NewRow();
  821. if (pValues.Length >= _XmlFieldNames.Length)
  822. throw new Exception("Invalid number of values"); //Extra Values
  823. for (int i = 0; i < _XmlFieldNames.Length; i++)
  824. {
  825. if (dr.Table.Columns[i].AutoIncrement) continue;
  826. if (pValues.Length == count) throw new Exception("Invalid number of values"); //Less number of values
  827. dr[i] = pValues[count];
  828. count++;
  829. }
  830. _DSXml.Tables[0].Rows.InsertAt(dr,pIndex);
  831. }
  832. else
  833. {
  834. if (pValues.Length == _XmlFieldNames.Length)
  835. {
  836. dr = _DSXml.Tables[0].NewRow();
  837. dr.ItemArray = pValues;
  838. _DSXml.Tables[0].Rows.InsertAt(dr, pIndex);
  839. }
  840. else
  841. throw new Exception("Invalid number of values");
  842. }
  843. }
  844.  
  845. /// <summary>
  846. /// Insert the specified xml node into the xml file at the specified index
  847. /// </summary>
  848. /// <param name="pIndex">Index where the values to be inserted</param>
  849. /// <param name="pXN">Valid xml node</param>
  850. public void Insert(int pIndex, XmlNode pXN)
  851. {
  852. try
  853. {
  854. this.Insert(pIndex, pXN.OuterXml);
  855. }
  856. catch (Exception ex)
  857. {
  858. throw ex;
  859. }
  860. }
  861.  
  862. /// <summary>
  863. /// Insert the specified data row into the xml file at the specified index
  864. /// </summary>
  865. /// <param name="pIndex">Index where the values to be inserted</param>
  866. /// <param name="pDR">DataRow</param>
  867. public void Insert(int pIndex, DataRow pDR)
  868. {
  869. try
  870. {
  871. this.Insert(pIndex, pDR.ItemArray);
  872. }
  873. catch (Exception ex)
  874. {
  875. throw ex;
  876. }
  877. }
  878.  
  879. /// <summary>
  880. /// Insert the specified xml data into the xml file at the specified index
  881. /// </summary>
  882. /// <param name="pIndex">Index where the values to be inserted</param>
  883. /// <param name="pXml">Valid xml string</param>
  884. public void InsertXML(int pIndex, string pXml)
  885. {
  886. try
  887. {
  888. if (IsValidXml(pXml)) this.Insert(pIndex, ConvertXMLToDataSet(pXml).Tables[0].Rows[0]);
  889. }
  890. catch (Exception ex)
  891. {
  892. throw ex;
  893. }
  894. }
  895.  
  896. #endregion
  897.  
  898. #region Set & Remove
  899. /// <summary>
  900. /// Setting the key field
  901. /// </summary>
  902. /// <param name="pFieldName"></param>
  903. public void SetKeyField(string pFieldName)
  904. {
  905. try
  906. {
  907. DataColumn[] dtColumns = new DataColumn[1];
  908. dtColumns[0] = _DSXml.Tables[0].Columns[pFieldName];
  909. _DSXml.Tables[0].PrimaryKey = dtColumns;
  910. _XmlKeyField = pFieldName;
  911. _IsKeyField = true;
  912. }
  913. catch (Exception ex)
  914. {
  915. throw ex;
  916. }
  917. }
  918.  
  919. /// <summary>
  920. /// Remove the key field
  921. /// </summary>
  922. public void RemoveKeyField()
  923. {
  924. _DSXml.Tables[0].PrimaryKey = null;
  925. _XmlKeyField = "";
  926. _IsKeyField = false;
  927. }
  928.  
  929. /// <summary>
  930. /// Set the auto increment setting to the specified field
  931. /// </summary>
  932. /// <param name="pFieldName">Field name</param>
  933. public void SetAutoIncrement(string pFieldName)
  934. {
  935. try
  936. {
  937. int maxValue = 0,value=0;
  938. _DSXml.Tables[0].Columns[pFieldName].AutoIncrement = true;
  939. if(_DSXml.Tables[0].Rows.Count>0)
  940. maxValue = Convert.ToInt32(_DSXml.Tables[0].Rows[0][pFieldName].ToString());
  941. for (int i = 1; i < _DSXml.Tables[0].Rows.Count; i++)
  942. {
  943. value = Convert.ToInt32(_DSXml.Tables[0].Rows[i][pFieldName].ToString());
  944. if (maxValue < value) maxValue = value;
  945. }
  946. _DSXml.Tables[0].Columns[pFieldName].AutoIncrementSeed = maxValue+1;
  947. _DSXml.Tables[0].Columns[pFieldName].AutoIncrementStep = 1;
  948. _IsIdentity = true;
  949. }
  950. catch (Exception ex)
  951. {
  952. throw ex;
  953. }
  954. }
  955.  
  956. /// <summary>
  957. /// Set the auto increment setting to the specified field
  958. /// </summary>
  959. /// <param name="pFieldName">Field name</param>
  960. /// <param name="pIncrementSeed">Increment Seed</param>
  961. /// <param name="pIncrementStep">Increment Step</param>
  962. public void SetAutoIncrement(string pFieldName, int pIncrementSeed, int pIncrementStep)
  963. {
  964. try
  965. {
  966. _DSXml.Tables[0].Columns[pFieldName].AutoIncrement = true;
  967. _DSXml.Tables[0].Columns[pFieldName].AutoIncrementSeed = pIncrementSeed;
  968. _DSXml.Tables[0].Columns[pFieldName].AutoIncrementStep = pIncrementStep;
  969. _IsIdentity = true;
  970. }
  971. catch (Exception ex)
  972. {
  973. throw ex;
  974. }
  975. }
  976.  
  977. /// <summary>
  978. /// Remove auto increment setting for the specified field
  979. /// </summary>
  980. /// <param name="pFieldName">Field name</param>
  981. public void RemoveAutoIncrement(string pFieldName)
  982. {
  983. int i = 0;
  984. _DSXml.Tables[0].Columns[pFieldName].AutoIncrement = false;
  985. for (i = 0; i < _DSXml.Tables[0].Columns.Count; i++)
  986. {
  987. if (_DSXml.Tables[0].Columns[i].AutoIncrement)
  988. {
  989. _IsIdentity = true;
  990. break;
  991. }
  992. }
  993. if (i >= _DSXml.Tables[0].Columns.Count) _IsIdentity = false;
  994. }
  995.  
  996. /// <summary>
  997. /// Remove auto increment setting for all fields
  998. /// </summary>
  999. public void RemoveAutoIncrement()
  1000. {
  1001. for (int i = 0; i < _DSXml.Tables[0].Columns.Count; i++) _DSXml.Tables[0].Columns[i].AutoIncrement = false;
  1002. _IsIdentity = false;
  1003. }
  1004. #endregion
  1005.  
  1006. #region Transaction
  1007. public void Commit()
  1008. {
  1009. HttpContext.Current.Application.Lock();
  1010. _DSXml.WriteXml(_XmlFileName);
  1011. HttpContext.Current.Application.UnLock();
  1012. }
  1013.  
  1014. public void RollBack()
  1015. {
  1016. _DSXml.ReadXml(_XmlFileName);
  1017. }
  1018. #endregion
  1019. #endregion
  1020.  
  1021. #region Public Static Method
  1022. public static DataSet ConvertXMLToDataSet(string XmlString)
  1023. {
  1024. StringReader SR = null;
  1025. XmlTextReader XTR = null;
  1026. try
  1027. {
  1028. DataSet DS = new DataSet();
  1029. SR = new StringReader(XmlString);
  1030. XTR = new XmlTextReader(SR);
  1031. DS.ReadXml(XTR);
  1032. return DS;
  1033. }
  1034. catch (Exception ex)
  1035. {
  1036. throw ex;
  1037. }
  1038. finally
  1039. {
  1040. if (XTR != null) XTR.Close();
  1041. }
  1042. }
  1043.  
  1044. public static string ConvertRawXMLToShowHTML(string XmlString)
  1045. {
  1046. return XmlString.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("\n","<br/>");
  1047. }
  1048.  
  1049. public static int RandomNumber(int min, int max)
  1050. {
  1051. Random random = new Random();
  1052. return random.Next(min, max);
  1053. }
  1054.  
  1055. public static string RandomString(int size, bool lowerCase)
  1056. {
  1057. StringBuilder builder = new StringBuilder();
  1058. Random random = new Random();
  1059. char ch;
  1060. for (int i = 0; i < size; i++)
  1061. {
  1062. ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
  1063. builder.Append(ch);
  1064. }
  1065. if (lowerCase)
  1066. return builder.ToString().ToLower();
  1067. return builder.ToString();
  1068. }
  1069.  
  1070. #endregion
  1071.  
  1072. #region Private Methods
  1073. private bool IsValidFile()
  1074. {
  1075. try
  1076. {
  1077. FileInfo fileInfo = new FileInfo(_XmlFileName);
  1078. if (fileInfo.Extension.ToLower() != ".xml")
  1079. throw new Exception(_XmlFileName + " should be a xml file");
  1080. if (!fileInfo.Exists && !_FileAutoGenerated)
  1081. throw new Exception(_XmlFileName + " does not exist");
  1082. if (!fileInfo.Exists)
  1083. {
  1084. _XmlDoc = new XmlDocument();
  1085. _XmlDoc.LoadXml("<" + RootElement + "></" + RootElement + ">");
  1086. _XmlDoc.Save(_XmlFileName);
  1087. }
  1088. _DSXml = new DataSet();
  1089. _DSXml.ReadXml(_XmlFileName);
  1090. if (_DSXml.Tables.Count > 1)
  1091. throw new Exception(_XmlFileName + " is not in a valid format");
  1092. else if (_DSXml.Tables.Count > 0)
  1093. {
  1094. if (_DSXml.DataSetName != _RootElement)
  1095. throw new Exception("Invalid root element");
  1096. if (_DSXml.Tables[0].TableName != _SubRootElement)
  1097. throw new Exception("Invalid sub root element");
  1098. if (_DSXml.Tables[0].Columns.Count != _XmlFieldNames.Length)
  1099. throw new Exception("Invalid number of fields");
  1100. for (int i = 0; i < _DSXml.Tables[0].Columns.Count; i++)
  1101. {
  1102. if (_DSXml.Tables[0].Columns[i].ColumnName != _XmlFieldNames.GetValue(i).ToString())
  1103. throw new Exception("Invalid field names");
  1104. }
  1105. }
  1106. else
  1107. {
  1108. if (_DSXml.DataSetName != _RootElement)
  1109. throw new Exception("Invalid root element");
  1110. DataTable dt = new DataTable(_SubRootElement);
  1111. for (int i = 0; i < _XmlFieldNames.Length; i++) dt.Columns.Add(_XmlFieldNames.GetValue(i).ToString());
  1112. _DSXml.Tables.Add(dt);
  1113. }
  1114. return true;
  1115. }
  1116. catch (Exception ex)
  1117. {
  1118. throw ex;
  1119. }
  1120. }
  1121. private bool IsValidXml(string pXml)
  1122. {
  1123. try
  1124. {
  1125. DataSet D=new DataSet();
  1126. D = ConvertXMLToDataSet(pXml);
  1127. if(D.Tables.Count!=1) throw new Exception("Invalid xml");
  1128. if (_IsIdentity)
  1129. {
  1130. int count = 0;
  1131. if (D.Tables[0].Columns.Count > _XmlFieldNames.Length)
  1132. throw new Exception("Invalid number of xml fields");
  1133. for (int i = 0; i < _XmlFieldNames.Length; i++)
  1134. {
  1135. if (_DSXml.Tables[0].Columns[i].AutoIncrement) continue;
  1136. if (count == D.Tables[0].Columns.Count) throw new Exception("Invalid number of fields");
  1137. if (D.Tables[0].Columns[count].ColumnName != _XmlFieldNames.GetValue(i).ToString())
  1138. throw new Exception("Invalid xml fields or the fields may be in disorder");
  1139. count++;
  1140. }
  1141. }
  1142. else
  1143. {
  1144. if (D.Tables[0].Columns.Count != _XmlFieldNames.Length)
  1145. throw new Exception("Invalid number of xml fields");
  1146. for (int i = 0; i < _XmlFieldNames.Length; i++)
  1147. {
  1148. if (D.Tables[0].Columns[i].ColumnName != _XmlFieldNames.GetValue(i).ToString())
  1149. throw new Exception("Invalid xml fields or the fields may be in disorder");
  1150. }
  1151. }
  1152. return true;
  1153. }
  1154. catch(Exception ex)
  1155. {
  1156. throw ex;
  1157. }
  1158. }
  1159. private void ConfigureXmlHandler(string pXmlFile, string pRootElement, string pSubRootElement, params string[] pFieldNames)
  1160. {
  1161. _RootElement = pRootElement;
  1162. _SubRootElement = pSubRootElement;
  1163. _XmlFieldNames = pFieldNames;
  1164. _FieldCount = _XmlFieldNames.Length;
  1165. _XmlFileName = pXmlFile;
  1166. _XmlKeyField = "";
  1167. _IsIdentity = false;
  1168. _IsKeyField = false;
  1169. if (_XmlFieldNames == null) throw new Exception("Xml file can not be without fields");
  1170. if (_XmlFieldNames.Length == 0) throw new Exception("Xml file can not be without fields");
  1171. if (IsValidFile())
  1172. {
  1173. if (_DSXml.Tables.Count > 0)
  1174. _RecordCount = _DSXml.Tables[0].Rows.Count;
  1175. else
  1176. _RecordCount = 0;
  1177. }
  1178. }
  1179. public void SetDataType(params Type[] pTypes)
  1180. {
  1181. try
  1182. {
  1183. string keyField = "";
  1184. if (_XmlFieldNames.Length == pTypes.Length)
  1185. {
  1186. DataSet ds = new DataSet(_RootElement);
  1187. DataTable dt = new DataTable(_SubRootElement);
  1188. for (int i = 0; i < _XmlFieldNames.Length; i++) dt.Columns.Add(_XmlFieldNames[i], pTypes[i]);
  1189. ds.Tables.Add(dt);
  1190. for (int i = 0; i < _DSXml.Tables[0].Rows.Count; i++) ds.Tables[0].Rows.Add(_DSXml.Tables[0].Rows[i].ItemArray);
  1191. for (int i = 0; i < _XmlFieldNames.Length; i++)
  1192. {
  1193. ds.Tables[0].Columns[i].AutoIncrement = _DSXml.Tables[0].Columns[i].AutoIncrement;
  1194. ds.Tables[0].Columns[i].AutoIncrementSeed = _DSXml.Tables[0].Columns[i].AutoIncrementSeed;
  1195. ds.Tables[0].Columns[i].AutoIncrementStep = _DSXml.Tables[0].Columns[i].AutoIncrementStep;
  1196. }
  1197. if (_IsKeyField)
  1198. {
  1199. DataColumn[] dtColumns = _DSXml.Tables[0].PrimaryKey;
  1200. DataColumn dtColumn = (DataColumn)dtColumns.GetValue(0);
  1201. keyField = dtColumn.ColumnName;
  1202. }
  1203. _DSXml = ds;
  1204. if (keyField != "") this.SetKeyField(keyField);
  1205. _RecordCount = _DSXml.Tables[0].Rows.Count;
  1206. _XmlFieldTypes = pTypes;
  1207. }
  1208. else
  1209. {
  1210. throw new Exception("No of columns differ from given data types");
  1211. }
  1212. }
  1213. catch (Exception ex)
  1214. {
  1215. throw ex;
  1216. }
  1217. }
  1218. #endregion
  1219. }
  1220. }

Report this snippet  

You need to login to post a comment.