XMLHelper.cs 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  1. using System.Collections.Specialized;
  2. using System.Data.Common;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. using System.Xml;
  6. using System.Xml.Schema;
  7. using System.Xml.XPath;
  8. using System.Xml.Xsl;
  9. namespace com.cloudsoft.utils
  10. {
  11. /// <summary>
  12. /// Class contains methods for constructing and querying XML doms
  13. /// </summary>
  14. ///
  15. public class XMLHelper
  16. {
  17. // Declare module level variables
  18. /// <summary>The Node that this XMLHelper wraps</summary>
  19. protected XmlNode omNode;
  20. protected Boolean isValidXml = true;
  21. public String ValidationError;
  22. private static String _QUOTE = "\"";
  23. private Regex _DatePattern = null;
  24. private Regex _DoublePattern = null;
  25. internal DbConnection _con = null;
  26. #region Constructors
  27. /// <summary>Creates a new instance of XMLHelper </summary>
  28. public XMLHelper()
  29. {
  30. }
  31. /// <summary>
  32. /// Creates a new instance of XMLHelper with the base node set
  33. /// </summary>
  34. /// <param name="zRoot">The root node to create or an XML Document to load</param>
  35. public XMLHelper(String zRoot)
  36. {
  37. if (zRoot.Contains("<") || zRoot.Contains("&#60;") || zRoot.Contains("\\"))
  38. //WADS: Cross site scripting...
  39. load(zRoot);
  40. else
  41. //WADS: XPath Injection...
  42. createRoot(zRoot);
  43. }
  44. public XMLHelper(StringBuilder zRoot)
  45. {
  46. load(zRoot);
  47. }
  48. public XMLHelper(Stream stream)
  49. {
  50. load(stream);
  51. }
  52. /// <summary>
  53. /// Creates a new instance of XMLHelper with the base node set
  54. /// </summary>
  55. /// <param name="oNode">The base node to set</param>
  56. public XMLHelper(XmlNode oNode)
  57. {
  58. omNode = oNode;
  59. }
  60. /// <summary>
  61. /// Creates a new instance of XMLHelper with the base node set to the first child of the Document
  62. /// </summary>
  63. /// <param name="oDoc">Document to base this XMLHelper on</param>
  64. public XMLHelper(XmlDocument oDoc)
  65. {
  66. omNode = (XmlNode)oDoc.DocumentElement;
  67. }
  68. #endregion
  69. #region IO & Save Methods
  70. /// <summary>
  71. /// Saves the dom to the specified path. Always overwrites anything already there.
  72. /// </summary>
  73. /// <param name="path">The path to save to.</param>
  74. public void SaveAs(String path)
  75. {
  76. try
  77. {
  78. //Overwrite any existing files...
  79. FileInfo fi = new FileInfo(path);
  80. if (fi.Exists)
  81. fi.Delete();
  82. }
  83. catch
  84. {
  85. //If you can't delete the file then don't try and overwrite it...
  86. return;
  87. }
  88. StreamWriter sw = new StreamWriter(path);
  89. try
  90. {
  91. //Set up a stream...
  92. sw.AutoFlush = true;
  93. //Write to the stream and close it...
  94. sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
  95. sw.Write(this.getXML());
  96. }
  97. finally
  98. {
  99. sw.Close();
  100. sw.Dispose();
  101. }
  102. }
  103. #endregion
  104. #region "Helper" methods
  105. /// <summary>
  106. /// Creates a Standard Request DOM
  107. /// </summary>
  108. /// <param name="zType">The type of request</param>
  109. /// <param name="zMudID">The mud id</param>
  110. /// <param name="zSystem">The system id</param>
  111. /// <returns></returns>
  112. public XMLHelper createStandardRequest(String zType, String zMudID, String zSystem)
  113. {
  114. return createStandardRequest(zType, zMudID, zSystem, zMudID);
  115. }
  116. /// <summary>
  117. /// Handles delegation
  118. /// </summary>
  119. /// <param name="zType"></param>
  120. /// <param name="zMudID"></param>
  121. /// <param name="zSystem"></param>
  122. /// <param name="UserLogon"></param>
  123. /// <returns></returns>
  124. public XMLHelper createStandardRequest(String zType, String zMudID, String zSystem, String UserLogon)
  125. {
  126. this.createRoot("request");
  127. this.appendAttribute("type", zType);
  128. this.appendNode("mud_id", zMudID);
  129. this.appendNode("system", zSystem);
  130. string zDeligateUser = UserLogon;
  131. string[] aSplit = zDeligateUser.Split("\\".ToCharArray());
  132. zDeligateUser = aSplit[aSplit.Length - 1].ToUpper();
  133. if (zMudID != zDeligateUser)
  134. this.appendNode("delegate_id", zDeligateUser);
  135. return this;
  136. }
  137. #endregion
  138. #region Dom Manipulation
  139. /// <summary>
  140. /// Creates the root Node
  141. /// </summary>
  142. /// <param name="zRootName">The name of the root node</param>
  143. public void createRoot(String zRootName)
  144. {
  145. //WADS: XPath Injection...
  146. this.load("<" + zRootName + "/>");
  147. }
  148. /// <summary>
  149. /// Appends a node to the dom
  150. /// </summary>
  151. /// <param name="vNodeName">The name of the new node</param>
  152. /// <returns>Returns an XMLHelper of the new Node</returns>
  153. public XMLHelper appendNode(String vNodeName)
  154. {
  155. XmlDocument oDoc = omNode.OwnerDocument;
  156. XmlNode oNewNode = oDoc.CreateElement(vNodeName);
  157. omNode.AppendChild(oNewNode);
  158. XMLHelper oHelper = new XMLHelper(oNewNode);
  159. return oHelper;
  160. }
  161. /// <summary>
  162. /// Appends a node to the dom and sets it's value
  163. /// </summary>
  164. /// <param name="vNodeName">The name of the new node</param>
  165. /// <param name="zValue">The value to set the new node</param>
  166. /// <returns>Returns the XMLHelper for the new node</returns>
  167. public XMLHelper appendNode(String vNodeName, Object zValue)
  168. {
  169. XmlDocument oDoc = omNode.OwnerDocument;
  170. XmlNode oNewNode = oDoc.CreateElement(vNodeName);
  171. oNewNode.AppendChild(oDoc.CreateTextNode(zValue == null ? "" : zValue.ToString()));
  172. omNode.AppendChild(oNewNode);
  173. XMLHelper oHelper = new XMLHelper(oNewNode);
  174. return oHelper;
  175. }
  176. /// <summary>
  177. /// Appends an XMLHelper dom
  178. /// </summary>
  179. /// <param name="oDOM">The DOM to append to this DOM</param>
  180. /// <returns>Returns the XMLHelper for the new node</returns>
  181. public XMLHelper appendNode(XMLHelper oDOM)
  182. {
  183. XmlNode oNew = omNode.OwnerDocument.ImportNode(oDOM.omNode, true);
  184. omNode.AppendChild(oNew);
  185. return new XMLHelper(oNew);
  186. }
  187. /// <summary>
  188. /// Appends a NameValueCollection to the DOM as Nodes
  189. /// </summary>
  190. /// <param name="oCol">The DOM to append to this DOM</param>
  191. public void appendNode(NameValueCollection oCol)
  192. {
  193. // Iterate through the collection and add
  194. // each name value pair to the dom
  195. for (int i = 0; i < oCol.Count; i++)
  196. {
  197. String zName = oCol.GetKey(i);
  198. String[] pValues = oCol.GetValues(i);
  199. for (int j = 0; j < pValues.Length; j++)
  200. appendNode(zName, pValues[j]);
  201. }
  202. }
  203. /// <summary>
  204. /// Appends an array of XMLHelpers
  205. /// </summary>
  206. /// <param name="oNodes">The Nodelist to append to the existing DOM</param>
  207. public void appendNode(XMLHelper[] oNodes)
  208. {
  209. for (int i = 0; i < oNodes.Length; i++)
  210. appendNode(oNodes[i]);
  211. }
  212. /// <summary>
  213. /// Appends an attribute to the dom and sets it's value
  214. /// </summary>
  215. /// <param name="zNodeName">The name of the new node</param>
  216. /// <param name="zValue">The value to set the new node</param>
  217. public void appendAttribute(String zNodeName, String zValue)
  218. {
  219. // Attr oAttr = omNode.getOwnerDocument().createAttribute(zNodeName);
  220. // oAttr.setNodeValue(zValue);
  221. // omNode.appendChild(oAttr);
  222. XmlElement oNode = (XmlElement)omNode;
  223. oNode.SetAttribute(zNodeName, zValue);
  224. }
  225. /// <summary>
  226. /// Retrieved the root node of a XMLHelper dom
  227. /// </summary>
  228. /// <returns>Returns the Root Node for this XMLHelper DOM</returns>
  229. public XMLHelper getRootNode()
  230. {
  231. return new XMLHelper(omNode.OwnerDocument);
  232. }
  233. public void appendXPathNode(String zXPath, String zValue)
  234. {
  235. appendXPathNode(zXPath, zValue, true);
  236. }
  237. /// <summary>
  238. /// Appends a node/attribute based on an XPath (like alias names in SQL)
  239. /// </summary>
  240. /// <param name="zXPath">The name of the new node/attribute in XPath syntax</param>
  241. /// <param name="zValue">The value to set the new node</param>
  242. public void appendXPathNode(String zXPath, String zValue, Boolean AlwaysAddNewNode)
  243. {
  244. try
  245. {
  246. int i;
  247. XmlNode oOrig = omNode;
  248. // Split the Name by "/"
  249. String[] arrNodes = zXPath.Split(new char[] { '/' });
  250. for (i = 0; i < arrNodes.Length - 1; i++)
  251. {
  252. XmlNode oNode = omNode.SelectSingleNode(arrNodes[i]);
  253. if (oNode == null)
  254. omNode = appendNode(arrNodes[i]).omNode;
  255. else
  256. omNode = oNode;
  257. }
  258. if (arrNodes[i].StartsWith("@"))
  259. appendAttribute(arrNodes[i].Substring(1), zValue);
  260. else
  261. {
  262. if (AlwaysAddNewNode)
  263. {
  264. appendNode(arrNodes[i], zValue);
  265. }
  266. else
  267. {
  268. if (this.nodeCount(arrNodes[i]) > 0)
  269. {
  270. this.getNode(arrNodes[i]).setValue(zValue);
  271. }
  272. else
  273. {
  274. appendNode(arrNodes[i], zValue);
  275. }
  276. }
  277. }
  278. omNode = oOrig;
  279. }
  280. catch (Exception ex)
  281. {
  282. System.Console.WriteLine(ex.Message);
  283. }
  284. }
  285. /// <summary>
  286. /// Removes a node given the nodename
  287. /// </summary>
  288. /// <param name="zNodeName">The name of node to remove</param>
  289. public void removeNode(String zNodeName)
  290. {
  291. try
  292. {
  293. XmlNode oTempNode = omNode.SelectSingleNode(zNodeName);
  294. if (oTempNode != null)
  295. oTempNode.ParentNode.RemoveChild(oTempNode);
  296. }
  297. catch
  298. {
  299. // Do nothing if we can't remove the node
  300. }
  301. }
  302. #endregion
  303. #region Serialisation
  304. /// <summary>
  305. /// Searalises the current tree from the current Node to a Writer (like Resonse.Output)
  306. /// </summary>
  307. /// <param name="oWriter">The Writer to write the XML to (eg. Resonse.Output)</param>
  308. public void serializeNode(TextWriter oWriter)
  309. {
  310. try
  311. {
  312. serializeNode(omNode, oWriter);
  313. }
  314. catch (Exception ex)
  315. {
  316. System.Console.WriteLine(ex.Message);
  317. }
  318. }
  319. /// <summary>
  320. /// Serializes the node.
  321. /// </summary>
  322. /// <param name="oNode">The node.</param>
  323. /// <param name="oWriter">The writer.</param>
  324. private void serializeNode(XmlNode oNode, TextWriter oWriter)
  325. {
  326. try
  327. {
  328. oWriter.Write(oNode.OuterXml);
  329. }
  330. catch (Exception ex)
  331. {
  332. System.Console.WriteLine(ex.Message);
  333. }
  334. }
  335. /// <summary>
  336. /// Serializes the node.
  337. /// </summary>
  338. /// <param name="oNode">The node.</param>
  339. /// <returns>An XML String</returns>
  340. private String serializeNode(XmlNode oNode)
  341. {
  342. try
  343. {
  344. return (oNode.OuterXml);
  345. }
  346. catch (Exception ex)
  347. {
  348. System.Console.WriteLine(ex.Message);
  349. return ("");
  350. }
  351. }
  352. public String getJSON()
  353. {
  354. StringWriter stringOut = new StringWriter();
  355. getJSON(stringOut);
  356. return (stringOut.ToString());
  357. }
  358. public void getJSON(TextWriter oOut)
  359. {
  360. oOut.Write("{");
  361. getJSON(omNode.OwnerDocument.FirstChild, false, oOut);
  362. oOut.Write("}");
  363. }
  364. private void getJSON(XmlNode oNode, Boolean isArray, TextWriter oOut)
  365. {
  366. String zName = oNode.Name;
  367. if (zName.Equals("#text")) zName = "$";
  368. if (!isArray)
  369. oOut.Write(_QUOTE + zName + _QUOTE + ":");
  370. if (isJSONArray(oNode.ChildNodes))
  371. {
  372. oOut.Write("[");
  373. getJSON(oNode.ChildNodes, true, oOut);
  374. oOut.Write("]");
  375. }
  376. else if (oNode.NodeType == XmlNodeType.Attribute)
  377. {
  378. oOut.Write(getJSONValue(oNode.Value));
  379. }
  380. else if (!oNode.HasChildNodes && (oNode.Attributes == null || oNode.Attributes.Count == 0))
  381. {
  382. if (oNode.Value == null)
  383. oOut.Write("null");
  384. else
  385. oOut.Write(getJSONValue(oNode.Value));
  386. }
  387. else if (oNode.Attributes.Count == 0 && oNode.ChildNodes.Count == 1 && !oNode.FirstChild.HasChildNodes)
  388. {
  389. oOut.Write(getJSONValue(oNode.FirstChild.Value));
  390. }
  391. else
  392. {
  393. oOut.Write("{");
  394. getJSON(oNode.Attributes, oOut);
  395. if (oNode.Attributes.Count > 0 && oNode.ChildNodes.Count > 0)
  396. oOut.Write(",");
  397. getJSON(oNode.ChildNodes, false, oOut);
  398. oOut.Write("}");
  399. }
  400. }
  401. private void getJSON(XmlNodeList oNodeList, Boolean isArray, TextWriter oOut)
  402. {
  403. for (int i = 0; i < oNodeList.Count; i++)
  404. {
  405. XmlNode oNode = oNodeList.Item(i);
  406. if (i > 0) oOut.Write(",");
  407. getJSON(oNode, isArray, oOut);
  408. }
  409. }
  410. private void getJSON(XmlAttributeCollection oNodeMap, TextWriter oOut)
  411. {
  412. for (int i = 0; i < oNodeMap.Count; i++)
  413. {
  414. XmlNode oNode = oNodeMap.Item(i);
  415. if (i > 0) oOut.Write(",");
  416. getJSON(oNode, false, oOut);
  417. }
  418. }
  419. private String getJSONValue(String zValue)
  420. {
  421. zValue = zValue.Trim();
  422. if (DatePattern().IsMatch(zValue))
  423. {
  424. DateTime matchDate = DateTime.Now;
  425. try { matchDate = DateTime.Parse(zValue); }
  426. catch (Exception ex) { }
  427. String ret = "new Date(" + matchDate.Year + "," + (matchDate.Month - 1) + "," + matchDate.Day;
  428. if (zValue.Length > 11)
  429. ret += "," + matchDate.Hour + "," + matchDate.Minute + "," + matchDate.Second;
  430. ret += ")";
  431. return ret;
  432. }
  433. else if (zValue.Length <= 10)
  434. {
  435. try { return int.Parse(zValue).ToString(); }
  436. catch (Exception ex) { }
  437. if (DoublePattern().IsMatch(zValue))
  438. return zValue;
  439. //try { return Double.Parse(zValue).ToString();}
  440. //catch (Exception ex) {}
  441. }
  442. return _QUOTE + zValue.Replace("\\", "\\\\").Replace("\"", "\\\"") + _QUOTE;
  443. }
  444. private Boolean isJSONArray(XmlNodeList oNodeList)
  445. {
  446. if (oNodeList.Count < 1 || (oNodeList.Count == 1 && (oNodeList.Item(0).Name + "s") != oNodeList.Item(0).ParentNode.Name)) return (false);
  447. String zName = oNodeList.Item(0).Name;
  448. for (int i = 0; i < oNodeList.Count; i++)
  449. {
  450. if (!oNodeList.Item(i).Name.Equals(zName))
  451. return (false);
  452. }
  453. return (true);
  454. }
  455. private Regex DatePattern()
  456. {
  457. if (_DatePattern == null)
  458. {
  459. _DatePattern = new Regex("^(([0-9])|([0-2][0-9])|([3][0-1]))-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\\d{4}.*", RegexOptions.IgnoreCase);
  460. }
  461. return _DatePattern;
  462. }
  463. private Regex DoublePattern()
  464. {
  465. if (_DoublePattern == null)
  466. {
  467. _DoublePattern = new Regex("^[-+]?[0-9]*\\.?[0-9]+$");
  468. }
  469. return _DoublePattern;
  470. }
  471. #endregion
  472. #region Load Methods
  473. /// <summary>
  474. /// Loads a string into the DOM or a file. Either an XML string or a file location may be used
  475. /// </summary>
  476. /// <param name="zXML">Either the XML as a string or a file location</param>
  477. /// <returns>Boolean indicating success or failure</returns>
  478. public Boolean load(String zXML)
  479. {
  480. try
  481. {
  482. XmlDocument oDOM = new XmlDocument();
  483. if (zXML.StartsWith("<") || zXML.StartsWith("&#60;"))
  484. //WADS: XPath Injection...
  485. oDOM.LoadXml(zXML);
  486. else
  487. //WADS: XPath Injection...
  488. oDOM.Load(zXML);
  489. omNode = (XmlNode)oDOM.DocumentElement;
  490. return true;
  491. }
  492. catch (Exception ex)
  493. {
  494. throw new Exception("Can't load dom. Error: " + ex.Message);
  495. }
  496. }
  497. public Boolean load(Stream stream)
  498. {
  499. try
  500. {
  501. XmlDocument oDOM = new XmlDocument();
  502. oDOM.Load(stream);
  503. omNode = (XmlNode)oDOM.DocumentElement;
  504. return true;
  505. }
  506. catch (Exception ex)
  507. {
  508. throw new Exception("Can't load dom. Error: " + ex.Message);
  509. }
  510. }
  511. public Boolean load(StringBuilder zXML)
  512. {
  513. try
  514. {
  515. StringReader sr = new StringReader(zXML.ToString());
  516. XmlReader xr = XmlReader.Create(sr);
  517. XmlDocument oDOM = new XmlDocument();
  518. oDOM.Load(xr);
  519. omNode = (XmlNode)oDOM.DocumentElement;
  520. return true;
  521. }
  522. catch (Exception ex)
  523. {
  524. throw new Exception("Can't load dom. Error: " + ex.Message);
  525. }
  526. }
  527. public Boolean loadTSV(String zPath)
  528. {
  529. try
  530. {
  531. // Create an instance of StreamReader to read from a file.
  532. // The using statement also closes the StreamReader.
  533. this.createRoot("response");
  534. XMLHelper oRows = this.appendNode("rows");
  535. using (StreamReader sr = new StreamReader(zPath))
  536. {
  537. String[] aHeader = null;
  538. String line;
  539. // Read and display lines from the file until the end of
  540. // the file is reached.
  541. while ((line = sr.ReadLine()) != null)
  542. {
  543. // Get the header if we don't have it
  544. if (aHeader == null)
  545. {
  546. aHeader = line.Split('\t');
  547. }
  548. else
  549. {
  550. String[] aFields = line.Split('\t');
  551. int i = 0;
  552. XMLHelper oRow = oRows.appendNode("row");
  553. foreach (String zHeader in aHeader)
  554. oRow.appendNode(zHeader, aFields[i++]);
  555. }
  556. }
  557. }
  558. return true;
  559. }
  560. catch (Exception e)
  561. {
  562. // Let the user know what went wrong.
  563. throw new Exception("The file could not be read: " + e.Message);
  564. }
  565. }
  566. #endregion
  567. #region Get and Set Methods
  568. /// <summary>
  569. /// Retrieved the child nodes an XMLHelper dom
  570. /// </summary>
  571. /// <returns>Returns the Child nodes as an array</returns>
  572. public XMLHelper[] getChildNodes()
  573. {
  574. XmlNodeList oNodes = omNode.ChildNodes;
  575. XMLHelper[] arrNodes = new XMLHelper[oNodes.Count];
  576. for (int i = 0; i < oNodes.Count; i++)
  577. arrNodes[i] = new XMLHelper(oNodes.Item(i));
  578. return arrNodes;
  579. }
  580. public XMLHelper getFirstChild()
  581. {
  582. return new XMLHelper(omNode.FirstChild);
  583. }
  584. public DateTime getSQLDate(String zXPath)
  585. {
  586. return getDate(zXPath);
  587. }
  588. public DateTime getSQLDateTime(String zXPath)
  589. {
  590. return getDate(zXPath);
  591. }
  592. /// <summary>
  593. /// Gets the XML of the current node as a string
  594. /// </summary>
  595. /// <returns>Returns the XML of the current node as a string</returns>
  596. public String getXML()
  597. {
  598. return serializeNode(omNode);
  599. }
  600. /// <summary>
  601. /// Gets the InnerXML of the current node as a string
  602. /// </summary>
  603. /// <returns>Returns the InnerXML of the current node as a string</returns>
  604. public String getInnerXML()
  605. {
  606. return omNode.InnerXml;
  607. }
  608. /// <summary>
  609. /// Gets the XML of the root node as a string
  610. /// </summary>
  611. /// <returns>Returns the XML of the root node as a string</returns>
  612. public String getRootXML()
  613. {
  614. return omNode.OwnerDocument.OuterXml;
  615. }
  616. /// <summary>
  617. /// Get the parent XMLHelper
  618. /// </summary>
  619. /// <returns>Returns the Parent node as an XMLHelper object</returns>
  620. public XMLHelper getParent()
  621. {
  622. XMLHelper oHelper = new XMLHelper(omNode.ParentNode);
  623. return oHelper;
  624. }
  625. /// <summary>
  626. /// Returns the Name of a node or element
  627. /// </summary>
  628. /// <returns>The node name of the current node</returns>
  629. public String getName()
  630. {
  631. try
  632. {
  633. return omNode.Name;
  634. }
  635. catch (Exception)
  636. {
  637. return "";
  638. }
  639. }
  640. /// <summary>
  641. /// Returns the value of a node or element
  642. /// JF 12/2007 - Added a check for a null valued XMLHelper (with a null omNode)
  643. /// rather than relying on the exception handler.
  644. /// </summary>
  645. /// <returns>The nodevalue of the current node</returns>
  646. public String getValue()
  647. {
  648. if (omNode != null)
  649. {
  650. try
  651. {
  652. return omNode.InnerText;
  653. }
  654. catch (Exception)
  655. {
  656. return "";
  657. }
  658. }
  659. else
  660. {
  661. return "";
  662. }
  663. }
  664. /// <summary>
  665. /// Returns the value of a node or element as a string
  666. /// </summary>
  667. /// <param name="zXPath">A valid XPath to find a node</param>
  668. /// <returns>The nodevalue of the Node found as a string</returns>
  669. public String getValue(String zXPath)
  670. {
  671. return getNode(zXPath).getValue();
  672. }
  673. /// <summary>
  674. /// Returns the value of a node or element as a string
  675. /// </summary>
  676. /// <param name="zXPath">A valid XPath to find a node</param>
  677. /// <returns>The nodevalue of the Node found as a string</returns>
  678. public String getString(String zXPath)
  679. {
  680. return getValue(zXPath);
  681. }
  682. /// <summary>
  683. /// Returns the value of a node or element as a long
  684. /// </summary>
  685. /// <param name="zXPath">A valid XPath to find a node</param>
  686. /// <returns>The nodevalue of the Node found as a long</returns>
  687. public long getLong(String zXPath)
  688. {
  689. try
  690. {
  691. return Convert.ToInt64(getValue(zXPath).Trim());
  692. }
  693. catch (Exception)
  694. {
  695. return 0;
  696. }
  697. }
  698. /// <summary>
  699. /// Returns the value of a node or element as an int
  700. /// </summary>
  701. /// <param name="zXPath">A valid XPath to find a node</param>
  702. /// <returns>The nodevalue of the Node found as an int</returns>
  703. public int getInt(String zXPath)
  704. {
  705. try
  706. {
  707. String value = getValue(zXPath).Trim();
  708. if (value.Length > 0)
  709. {
  710. return Convert.ToInt32(value);
  711. }
  712. else
  713. {
  714. return 0;
  715. }
  716. }
  717. catch (Exception)
  718. {
  719. return 0;
  720. }
  721. }
  722. /// <summary>
  723. /// Returns the value of a node or element as a double
  724. /// </summary>
  725. /// <param name="zXPath">A valid XPath to find a node</param>
  726. /// <returns>The nodevalue of the Node found as a double</returns>
  727. public double getDouble(String zXPath)
  728. {
  729. try
  730. {
  731. return Convert.ToDouble(getValue(zXPath).Trim());
  732. }
  733. catch
  734. {
  735. return 0.0;
  736. }
  737. }
  738. /// <summary>
  739. /// Returns the value of a node or element as a DateTime Object
  740. /// </summary>
  741. /// <param name="zXPath">A valid XPath to find a node</param>
  742. /// <returns>The nodevalue of the Node found as a DateTime Object</returns>
  743. public DateTime getDate(String zXPath)
  744. {
  745. DateTime d = DateTime.MinValue;
  746. try
  747. {
  748. DateTime.TryParse(getValue(zXPath).Trim(), out d);
  749. }
  750. catch { }
  751. return d;
  752. }
  753. /// <summary>
  754. /// Returns the value of a node or element as a Boolean
  755. /// </summary>
  756. /// <param name="zXPath">A valid XPath to find a node</param>
  757. /// <returns>The nodevalue of the Node found as a Boolean</returns>
  758. public Boolean getBoolean(String zXPath)
  759. {
  760. String zVal = getValue(zXPath).ToUpper();
  761. return (zVal.Equals("Y") || zVal.Equals("YES") || zVal.Equals("TRUE"));
  762. }
  763. /// <summary>
  764. /// Returns the value of a node or element as either Y or N
  765. /// </summary>
  766. /// <param name="zXPath">A valid XPath to find a node</param>
  767. /// <returns>The nodevalue of the Node found as a Boolean</returns>
  768. public String getYN(String zXPath)
  769. {
  770. return (getBoolean(zXPath) ? "Y" : "N");
  771. }
  772. /// <summary>
  773. /// Returns an XMLHelper for a XPath
  774. /// </summary>
  775. /// <param name="zXPath">A valid XPath to find a node</param>
  776. /// <returns>XMLHelper for a XPath</returns>
  777. public XMLHelper getNode(String zXPath)
  778. {
  779. XmlNode oNode;
  780. try
  781. {
  782. oNode = omNode.SelectSingleNode(zXPath);
  783. return new XMLHelper(oNode);
  784. }
  785. catch
  786. {
  787. throw new Exception("Can't get node for xpath: " + zXPath);
  788. }
  789. }
  790. /// <summary>
  791. /// Returns an Array of XMLHelper objects for a XPath
  792. /// </summary>
  793. /// <param name="zXPath">A valid XPath to find a node</param>
  794. /// <returns>Array of XMLHelper object for a XPath</returns>
  795. public XMLHelper[] getNodes(String zXPath)
  796. {
  797. try
  798. {
  799. XmlNodeList oNodes = omNode.SelectNodes(zXPath);
  800. XMLHelper[] arrNodes = new XMLHelper[oNodes.Count];
  801. for (int i = 0; i < oNodes.Count; i++)
  802. arrNodes[i] = new XMLHelper(oNodes.Item(i));
  803. return arrNodes;
  804. }
  805. catch (Exception ex)
  806. {
  807. System.Console.WriteLine(ex.Message);
  808. return null;
  809. }
  810. }
  811. /// <summary>
  812. /// Gets a CSV string given an XPath
  813. /// </summary>
  814. /// <param name="zXPath">The XPath.</param>
  815. /// <returns>A CSV String</returns>
  816. public String getCSV(String zXPath)
  817. {
  818. StringWriter oSW = new StringWriter();
  819. XmlNodeList oNodes = omNode.SelectNodes(zXPath);
  820. for (int i = 0; i < oNodes.Count; i++)
  821. {
  822. if (i > 0)
  823. oSW.Write(',');
  824. oSW.Write(oNodes[i].InnerText);
  825. }
  826. return oSW.ToString();
  827. }
  828. /// <summary>
  829. /// Sets a Nodes value
  830. /// </summary>
  831. /// <param name="zNodeName">The name of the node to set</param>
  832. /// <param name="zValue">The value to set the node</param>
  833. public void setNode(String zNodeName, String zValue)
  834. {
  835. removeNode(zNodeName);
  836. appendNode(zNodeName, zValue);
  837. }
  838. /// <summary>
  839. /// Sets a Nodes value
  840. /// </summary>
  841. /// <param name="zNodeName">The name of the node to set</param>
  842. /// <param name="zValue">The value to set the node</param>
  843. public void setValue(String zValue)
  844. {
  845. omNode.InnerText = zValue;
  846. }
  847. /// <summary>
  848. /// Sets an attribute value
  849. /// </summary>
  850. /// <param name="zNodeName">The name of the attribute to set</param>
  851. /// <param name="zValue">The value to set the attribute</param>
  852. public void setAttribute(String zAttributeName, String zValue)
  853. {
  854. omNode.Attributes[zAttributeName].Value = zValue;
  855. }
  856. public void setAttribute(String zAttributeName, Int32 iValue)
  857. {
  858. omNode.Attributes[zAttributeName].Value = iValue.ToString();
  859. }
  860. /// <summary>
  861. /// Gets the numbers of rows given an XPath
  862. /// </summary>
  863. /// <param name="zXPath">The Xpath to get the nodecount for</param>
  864. /// <returns>The number of nodes for the given Xpath</returns>
  865. public int nodeCount(String zXPath)
  866. {
  867. try
  868. {
  869. XmlNodeList oNodes = omNode.SelectNodes(zXPath);
  870. return (oNodes.Count);
  871. }
  872. catch
  873. {
  874. return 0;
  875. }
  876. }
  877. public XPathNodeIterator getIterator(String sourceNodePath)
  878. {
  879. XPathNavigator nav = omNode.CreateNavigator();
  880. XPathExpression expression = nav.Compile(sourceNodePath);
  881. return nav.Select(expression);
  882. }
  883. #endregion
  884. #region Testing
  885. /// <summary>
  886. /// Test Harness
  887. /// </summary>
  888. /// <param name="args">the command line arguments</param>
  889. public static void Main(String[] args)
  890. {
  891. // Test simple Load
  892. XMLHelper oDOM = new XMLHelper();
  893. // Test creating a DOM by handing and manipulating it
  894. oDOM.createStandardRequest("test_type", "PYL38997", "CIMS");
  895. oDOM.appendNode("test", "value");
  896. System.Console.WriteLine("test:" + oDOM.getValue("test"));
  897. System.Console.WriteLine("@type:" + oDOM.getValue("@type"));
  898. oDOM.removeNode("test");
  899. System.Console.WriteLine(oDOM.getRootXML());
  900. // More manipulation
  901. oDOM.appendNode("test", "value");
  902. oDOM.setNode("test", "new Value");
  903. oDOM = oDOM.appendNode("groups");
  904. for (int i = 0; i < 10; i++)
  905. oDOM.appendNode("group").appendAttribute("id", Convert.ToString(i));
  906. // Test getNodes
  907. XMLHelper[] oNodes = oDOM.getNodes("//group");
  908. for (int i = 0; i < oNodes.Length; i++)
  909. System.Console.WriteLine(oNodes[i].getXML());
  910. System.Console.WriteLine(oDOM.getRootXML());
  911. // Test appending an entirely new DOM
  912. XMLHelper oNew = new XMLHelper("new");
  913. oNew.appendNode("test", "40213423");
  914. oDOM.appendNode(oNew);
  915. System.Console.WriteLine(oDOM.getRootXML());
  916. // Test appending a Nodelist
  917. oNew.appendNode(oNodes);
  918. System.Console.WriteLine(oNew.getRootXML());
  919. System.Console.WriteLine(oDOM.getRootNode().getChildNodes().Length);
  920. System.Console.WriteLine(oDOM.omNode.OwnerDocument.GetElementsByTagName("group").Count);
  921. System.Console.WriteLine(oDOM.getParent().getNode("group").getXML());
  922. XMLHelper o = new XMLHelper("response");
  923. o.appendXPathNode("customer/@id", "1");
  924. o.appendXPathNode("customer/name/first", "john");
  925. o.appendXPathNode("customer/name/last", "doe");
  926. o.appendNode("date", "01-jan-2005");
  927. o.appendNode("empty_date", "");
  928. System.Console.WriteLine(o.getDate("date"));
  929. System.Console.WriteLine(o.getDate("empty_date"));
  930. System.Console.WriteLine(o.getRootXML());
  931. System.Console.WriteLine(o.omNode.ChildNodes.Count);
  932. System.Console.WriteLine("Inner : " + o.getInnerXML());
  933. }
  934. #endregion
  935. #region Distinct XML
  936. /// <summary>
  937. /// Appends a distinct set of the specified nodes to the current document.
  938. /// See description for other appendDistinct overload (Example 1).
  939. /// </summary>
  940. public void appendDistinct(String sourceNodePath, String keyAttribute, String resultRoot)
  941. {
  942. appendDistinct(sourceNodePath, keyAttribute, resultRoot, null, "");
  943. }
  944. /// <summary>
  945. /// High performance option for append distinct.
  946. /// Allows direct access with an iterator.
  947. /// Useful if adding multiple distincts from the same source rowset.
  948. /// </summary>
  949. /// <param name="incomingIterator"></param>
  950. /// <param name="keyAttribute"></param>
  951. /// <param name="resultRoot"></param>
  952. /// <param name="additionalAttributes"></param>
  953. /// <param name="resultNodesName"></param>
  954. /// <param name="UseNodes">Forces the iterator to select nodes rather than attributes</param>
  955. public void appendDistinct(XPathNodeIterator incomingIterator, String keyAttribute, String resultRoot, String[] additionalAttributes, String resultNodesName, Boolean UseNodes)
  956. {
  957. string value;
  958. string attribute;
  959. SortedList<string, object> ids = new SortedList<string, object>();
  960. MemoryStream ms = new MemoryStream();
  961. XmlTextWriter xmlWriter = new XmlTextWriter(ms, Encoding.UTF8);
  962. xmlWriter.WriteStartElement(resultRoot);
  963. XPathNodeIterator iterator = incomingIterator.Clone();
  964. while (iterator.MoveNext())
  965. {
  966. if (UseNodes)
  967. attribute = keyAttribute;
  968. else
  969. attribute = "@" + keyAttribute;
  970. value = iterator.Current.SelectSingleNode(attribute).Value;
  971. if (!ids.ContainsKey(value))
  972. {
  973. if (additionalAttributes != null)
  974. {
  975. xmlWriter.WriteStartElement(resultNodesName);
  976. xmlWriter.WriteAttributeString(keyAttribute, value);
  977. ids.Add(value, null);
  978. foreach (String extraNode in additionalAttributes)
  979. {
  980. if (UseNodes)
  981. attribute = extraNode;
  982. else
  983. attribute = "@" + extraNode;
  984. xmlWriter.WriteAttributeString(extraNode, iterator.Current.SelectSingleNode(attribute).Value);
  985. }
  986. xmlWriter.WriteEndElement();
  987. }
  988. else
  989. {
  990. xmlWriter.WriteElementString(keyAttribute, value);
  991. ids.Add(value, null);
  992. }
  993. }
  994. }
  995. xmlWriter.WriteEndElement();
  996. xmlWriter.Flush();
  997. ms.Seek(0, System.IO.SeekOrigin.Begin);
  998. XmlDocument distinct = new XmlDocument();
  999. distinct.Load(ms);
  1000. xmlWriter.Close();
  1001. omNode.AppendChild(omNode.OwnerDocument.ImportNode(distinct.DocumentElement, true));
  1002. }
  1003. public void appendDistinct(XPathNodeIterator incomingIterator, String keyAttribute, String resultRoot, String[] additionalAttributes, String resultNodesName)
  1004. {
  1005. appendDistinct(incomingIterator, keyAttribute, resultRoot, additionalAttributes, resultNodesName, false);
  1006. }
  1007. /// <summary>
  1008. /// Appends a distinct set of the specified nodes to the current document.
  1009. /// If additionalAttributes is null then resultNodesName is not used and the results will look
  1010. /// like the first example. With additionalAttributes
  1011. /// </summary>
  1012. /// <param name="sourceNodePath">XPath to the nodes to be distincted.</param>
  1013. /// <param name="keyAttribute">Attribute to key on for determining uniqueness.</param>
  1014. /// <param name="resultRoot">Name of the root node to create and append the distinct nodes to.</param>
  1015. /// <param name="additionalAttributes">Additional attributes to include in the distinct items.</param>
  1016. /// <param name="resultNodesName">Name of individual result nodes.</param>
  1017. /// <param name="UseNodes">Use nodes instead of attributes for the data.</param>
  1018. ///
  1019. /// <example >
  1020. ///
  1021. /// Source:
  1022. /// <activities>
  1023. /// <activity customer_id="1" name="Phil" type="M"/>
  1024. /// <activity customer_id="1" name="Phil" type="C"/>
  1025. /// <activity customer_id="2" name="Trev" type="M"/>
  1026. /// </activities>
  1027. ///
  1028. /// Example 1: appendDistinct("activities/activity", "customer_id", "customer_ids")
  1029. /// <customer_ids execution_duration="0">
  1030. /// <customer_id>1</customer_id>
  1031. /// <customer_id>2</customer_id>
  1032. /// </customer_ids>
  1033. ///
  1034. /// Example 2: appendDistinct("activities/activity", "customer_id", "customers", new string[] { "name" }, "customer" )
  1035. /// <customers execution_duration="0">
  1036. /// <customer customer_id="1" name="Phil" />
  1037. /// <customer customer_id="2" name="Trev" />
  1038. /// </customers>
  1039. ///
  1040. /// </example>
  1041. public void appendDistinct(String sourceNodePath, String keyAttribute, String resultRoot, String[] additionalAttributes, String resultNodesName, Boolean UseNodes)
  1042. {
  1043. DateTime startTime = DateTime.Now;
  1044. XPathNavigator nav = omNode.CreateNavigator();
  1045. XPathExpression expression = nav.Compile(sourceNodePath);
  1046. XPathNodeIterator iterator = nav.Select(expression);
  1047. appendDistinct(iterator, keyAttribute, resultRoot, additionalAttributes, resultNodesName, UseNodes);
  1048. TimeSpan ExecutionDuration = DateTime.Now - startTime;
  1049. this.getNode(resultRoot).appendAttribute("execution_duration", ExecutionDuration.TotalMilliseconds.ToString());
  1050. }
  1051. public void appendDistinct(String sourceNodePath, String keyAttribute, String resultRoot, String[] additionalAttributes, String resultNodesName)
  1052. {
  1053. appendDistinct(sourceNodePath, keyAttribute, resultRoot, additionalAttributes, resultNodesName, false);
  1054. }
  1055. #endregion
  1056. #region Validation
  1057. public string validateXML(String schema)
  1058. {
  1059. StringReader srXml = new StringReader(getRootXML());
  1060. XmlValidatingReader vr;
  1061. XmlTextReader tr;
  1062. XmlSchemaCollection schemaCol = new XmlSchemaCollection();
  1063. XMLHelper oResponse = new XMLHelper("response");
  1064. try
  1065. {
  1066. schemaCol.Add(null, schema);
  1067. isValidXml = true;
  1068. // Read the xml.
  1069. tr = new XmlTextReader(srXml);
  1070. // Create the validator.
  1071. vr = new XmlValidatingReader(tr);
  1072. // Set the validation tyep.
  1073. vr.ValidationType = ValidationType.Auto;
  1074. // Add the schema.
  1075. if (schemaCol != null)
  1076. {
  1077. vr.Schemas.Add(schemaCol);
  1078. }
  1079. // Set the validation event handler.
  1080. vr.ValidationEventHandler +=
  1081. new ValidationEventHandler(ValidationCallBack);
  1082. // Read the xml schema.
  1083. while (isValidXml && vr.Read())
  1084. {
  1085. }
  1086. vr.Close();
  1087. oResponse.appendNode("valid", "true");
  1088. return oResponse.getRootXML();
  1089. }
  1090. catch (Exception ex)
  1091. {
  1092. oResponse.appendNode("valid", "false");
  1093. oResponse.appendNode("error", ex.Message);
  1094. ValidationError = ex.Message;
  1095. return oResponse.getRootXML();
  1096. }
  1097. finally
  1098. {
  1099. // Clean up...
  1100. vr = null;
  1101. tr = null;
  1102. schemaCol = null;
  1103. }
  1104. }
  1105. public Boolean validateUsing(String schema)
  1106. {
  1107. StringReader srXml = new StringReader(getRootXML());
  1108. XmlValidatingReader vr;
  1109. XmlTextReader tr;
  1110. XmlSchemaCollection schemaCol = new XmlSchemaCollection();
  1111. schemaCol.Add(null, schema);
  1112. isValidXml = true;
  1113. try
  1114. {
  1115. // Read the xml.
  1116. tr = new XmlTextReader(srXml);
  1117. // Create the validator.
  1118. vr = new XmlValidatingReader(tr);
  1119. // Set the validation tyep.
  1120. vr.ValidationType = ValidationType.Auto;
  1121. // Add the schema.
  1122. if (schemaCol != null)
  1123. {
  1124. vr.Schemas.Add(schemaCol);
  1125. }
  1126. // Set the validation event handler.
  1127. vr.ValidationEventHandler +=
  1128. new ValidationEventHandler(ValidationCallBack);
  1129. // Read the xml schema.
  1130. while (isValidXml && vr.Read())
  1131. {
  1132. }
  1133. vr.Close();
  1134. return isValidXml;
  1135. }
  1136. catch (Exception ex)
  1137. {
  1138. ValidationError = ex.Message;
  1139. return false;
  1140. }
  1141. finally
  1142. {
  1143. // Clean up...
  1144. vr = null;
  1145. tr = null;
  1146. }
  1147. }
  1148. private void ValidationCallBack(object sender,
  1149. ValidationEventArgs args)
  1150. {
  1151. // The xml does not match the schema.
  1152. isValidXml = false;
  1153. ValidationError = args.Message;
  1154. }
  1155. #endregion
  1156. #region XSL Transformation
  1157. /// <summary>
  1158. /// Transforms the current DOM using the XSL and returns an XMLHelper DOM
  1159. /// </summary>
  1160. /// <param name="zXSL">The XSL for the transform</param>
  1161. /// <returns>XMLHelper DOM with the result of the transform</returns>
  1162. public XMLHelper transform(String zXSL)
  1163. {
  1164. return transform(zXSL, new Dictionary<string, string>());
  1165. }
  1166. /// <summary>
  1167. /// Transforms the current DOM using the XSL to a TextWriter
  1168. /// </summary>
  1169. /// <param name="zXSL">The XSL for the transform</param>
  1170. /// <param name="oOut">Thre Textwriter to output to</param>
  1171. public void transform(String zXSL, TextWriter oOut)
  1172. {
  1173. transform(zXSL, new Dictionary<string, string>(), oOut);
  1174. }
  1175. public XMLHelper transform(Stream oXSL)
  1176. {
  1177. return transform(oXSL, new Dictionary<string, string>());
  1178. }
  1179. public XMLHelper transform(Stream oXSL, Dictionary<string, string> aParams)
  1180. {
  1181. StringWriter oText = new StringWriter();
  1182. transform(oXSL, aParams, oText);
  1183. return new XMLHelper(oText.GetStringBuilder());
  1184. }
  1185. /// <summary>
  1186. /// Transforms the current DOM using the XSL to a TextWriter
  1187. /// </summary>
  1188. /// <param name="zXSL">The XSL for the transform</param>
  1189. /// <param name="aParams">String Array of paramters to pass to the XSL</param>
  1190. /// <param name="oOut">Thre Textwriter to output to</param>
  1191. public void transform(String zXSL, Dictionary<string, string> aParams, TextWriter oOut)
  1192. {
  1193. Stream oXSL = new FileStream(zXSL, FileMode.Open);
  1194. transform(oXSL, aParams, oOut);
  1195. }
  1196. public void transform(Stream oXSL, Dictionary<string, string> aParams, TextWriter oOut)
  1197. {
  1198. XslCompiledTransform transformer = new XslCompiledTransform();
  1199. XsltArgumentList args = new XsltArgumentList();
  1200. foreach (string key in aParams.Keys)
  1201. {
  1202. args.AddParam(key, "", aParams[key]);
  1203. }
  1204. //Having to specify settings for the load, due to embedded script usage...
  1205. XsltSettings xsltSettings = new XsltSettings(true, true);
  1206. XmlUrlResolver xmlResolver = new XmlUrlResolver();
  1207. XmlReader reader = XmlReader.Create(oXSL);
  1208. transformer.Load(reader, xsltSettings, xmlResolver);
  1209. //transformer.Load(oXSL, );
  1210. if (omNode.ParentNode == omNode.OwnerDocument)
  1211. transformer.Transform(omNode.OwnerDocument, args, oOut);
  1212. else
  1213. transformer.Transform(omNode, args, oOut);
  1214. }
  1215. /// <summary>
  1216. /// Transforms the current DOM using the XSL and returns an XMLHelper DOM
  1217. /// </summary>
  1218. /// <param name="zXSL">The XSL for the transform</param>
  1219. /// <param name="aParams">String Array of paramters to pass to the XSL</param>
  1220. /// <returns>XMLHelper DOM with the result of the transform</returns>
  1221. public XMLHelper transform(String zXSL, Dictionary<string, string> aParams)
  1222. {
  1223. //try
  1224. //{
  1225. StringWriter oText = new StringWriter();
  1226. transform(zXSL, aParams, oText);
  1227. return new XMLHelper(oText.GetStringBuilder());
  1228. //}
  1229. //catch (Exception ex)
  1230. //{
  1231. // System.Console.WriteLine(ex.Message);
  1232. // return null;
  1233. //}
  1234. }
  1235. #endregion
  1236. }
  1237. }