We Recommend

Accelerated C# 2008 Accelerated C# 2008
This book is both a rapid tutorial and a permanent reference. You’ll quickly master C# syntax while learning how the CLR simplifies many programming tasks. You’ll also learn best practices that ensure your code will be efficient, reusable, and robust. Why spend months or years discovering the best ways to design and code C# when this book will show you how to do things the right way, right from the start?


Posted By

zvasanth on 12/29/07


Tagged

database data xml handler store datastore


Versions (?)


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(JScriptLover@gmail.com)
  6.   * Credits:Vasanthakumar(DrunkenProgrammer@gmail.com), Syed Vaisul Karne(SqlLover@gmail.com)
  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">Data