XMLHelper.cs 47 KB

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