XMLTool.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Collections;
  5. using System.Xml;
  6. namespace CommonUI.Tool
  7. {
  8. public class XMLTool
  9. {
  10. private static XMLTool mInstance = null;
  11. protected Hashtable mDocs = new Hashtable();
  12. public XMLTool()
  13. {
  14. mInstance = this;
  15. }
  16. public static XMLTool Instance
  17. {
  18. get
  19. {
  20. if (mInstance == null) { new XMLTool(); }
  21. return mInstance;
  22. }
  23. }
  24. public virtual XmlDocument LoadXML(string fileName)
  25. {
  26. if (mDocs.ContainsKey(fileName)) { return (XmlDocument)(mDocs[fileName]);}
  27. return null;
  28. }
  29. public virtual XmlDocument LoadLocalXML(string fileName)
  30. {
  31. return null;
  32. }
  33. public virtual XmlNode GetXmlNode(string fileName, string nodeName)
  34. {
  35. XmlDocument doc = LoadXML(fileName);
  36. XmlNode xml = doc.FirstChild;
  37. if (xml != null)
  38. {
  39. XmlNode data = xml.NextSibling;
  40. return GetXmlNode(nodeName, data);
  41. }
  42. else
  43. {
  44. return null;
  45. }
  46. }
  47. private XmlNode GetXmlNode(string nodeName, XmlNode root)
  48. {
  49. XmlNode result = null;
  50. if (root.Attributes["name"] != null && nodeName.Equals(root.Attributes["name"].Value))
  51. {
  52. result = root;
  53. return result;
  54. }
  55. XmlNode subNode = root.FirstChild;
  56. while (subNode != null && result == null)
  57. {
  58. result = GetXmlNode(nodeName, subNode);
  59. subNode = subNode.NextSibling;
  60. }
  61. return result;
  62. }
  63. public XmlNode FindChild(XmlNode e, string childName)
  64. {
  65. foreach (XmlNode cc in e.ChildNodes)
  66. {
  67. if (cc.Name == childName)
  68. {
  69. return cc;
  70. }
  71. }
  72. return null;
  73. }
  74. public XmlDocument GetXml(byte[] data_)
  75. {
  76. if (data_ != null)
  77. {
  78. XmlDocument xmlDoc = new XmlDocument();
  79. int l = System.Text.Encoding.UTF8.GetString(data_).Length;
  80. byte[] data = new byte[l];
  81. if (data_[0] != 60)
  82. Array.Copy(data_, 3, data, 0, l - 3);
  83. else
  84. data = data_;
  85. if (data != null)
  86. {
  87. string _data = System.Text.Encoding.UTF8.GetString(data);
  88. if (_data != null)
  89. xmlDoc.LoadXml(_data);
  90. data_ = null;
  91. data = null;
  92. //mDocs.Add(mFileName, xmlDoc);
  93. return xmlDoc;
  94. }
  95. }
  96. return null;
  97. }
  98. }
  99. }