rsXmlNode.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic ;
  4. using System.Xml;
  5. using System.Text;
  6. using System.IO;
  7. namespace SysBaseLibs
  8. {
  9. /// <summary>
  10. /// rsXmlNode 创建 xml 数据格式
  11. /// songyf 200606 v1.0.0
  12. /// </summary>
  13. public class rsXmlNode
  14. {
  15. public rsXmlNode(string pcName, string pcValue)
  16. {
  17. this._Name = "";
  18. this._Value = "";
  19. this._Depth = -1;
  20. this._Id = -1;
  21. this._Nodes = new List<rsXmlNode >();
  22. this._Name = pcName;
  23. this._Value = Utils.FormatXMLToCode(pcValue);
  24. this._Id = Utils.ValI(AppEnv.GetRandom());
  25. }
  26. // Fields
  27. private int _Depth;
  28. private int _Id;
  29. private string _Name;
  30. private List<rsXmlNode> _Nodes;
  31. private string _Value;
  32. private string _XmlDeclaration = "";
  33. /// <summary>
  34. /// xml 声明信息
  35. /// </summary>
  36. public string XmlDeclaration
  37. {
  38. get { return _XmlDeclaration; }
  39. set { _XmlDeclaration = value; }
  40. }
  41. private string _Comment = "";
  42. /// <summary>
  43. /// 该节点注释信息
  44. /// </summary>
  45. public string Comment
  46. {
  47. get { return _Comment; }
  48. set { _Comment = value; }
  49. }
  50. // Methods
  51. public void AddChild(rsXmlNode poNode)
  52. {
  53. this._Nodes.Add(poNode);
  54. }
  55. public override bool Equals(object obj)
  56. {
  57. return (this.GetHashCode() == obj.GetHashCode());
  58. }
  59. public rsXmlNode GetChildNode(string pcNodeName)
  60. {
  61. string text1 = "";
  62. return this.GetChildNode(pcNodeName, ref text1, false);
  63. }
  64. public rsXmlNode GetChildNode(string pcNodeName, ref string pcMessage, bool plMandatory)
  65. {
  66. rsXmlNode node1 = null;
  67. foreach (rsXmlNode node2 in this._Nodes)
  68. {
  69. if (UtilStr.UAndT(node2.Name) == UtilStr.UAndT(pcNodeName))
  70. {
  71. node1 = node2;
  72. break;
  73. }
  74. }
  75. if ((node1 == null) && plMandatory)
  76. {
  77. pcMessage = pcMessage + ((pcMessage == "") ? "" : "\r\n") +pcNodeName;// AppEnv.T(0x18767, pcNodeName);
  78. }
  79. return node1;
  80. }
  81. public string GetChildValue(string pcNodeName)
  82. {
  83. string text1 = "";
  84. rsXmlNode node1 = this.GetChildNode(pcNodeName);
  85. if (node1 != null)
  86. {
  87. text1 = node1.Value.Trim();
  88. }
  89. return text1;
  90. }
  91. public override int GetHashCode()
  92. {
  93. return this._Id.GetHashCode();
  94. }
  95. public override string ToString()
  96. {
  97. string text1 = this.Name;
  98. if (this.Value != "")
  99. {
  100. text1 = text1 + "=" + this.Value;
  101. }
  102. return text1;
  103. }
  104. //public string ToXmlStringNoTrans()
  105. //{
  106. // if (this.Nodes.Count == 0)
  107. // {
  108. // string text2 = "<{0}>{1}</{0}>";
  109. // return string.Format(text2, this.Name, this.Value);
  110. // }
  111. // string text3 = "<{0}>\r\n{1}\r\n</{0}>";
  112. // string text4 = "";
  113. // foreach (rsXmlNode node1 in this.Nodes)
  114. // {
  115. // text4 = text4 + ((text4 == "") ? "" : "\r\n") + node1.ToXmlStringNoTrans();
  116. // }
  117. // return string.Format(text3, this.Name, text4);
  118. //}
  119. public string ToXmlString()
  120. {
  121. if (this.Nodes.Count == 0)
  122. {
  123. string text2 = "<{0}>{1}</{0}>";
  124. return string.Format(text2, this.Name, FormatCodeToXML(this.Value));
  125. }
  126. string text3 = "<{0}>\r\n{1}\r\n</{0}>";
  127. string text4 = "";
  128. foreach (rsXmlNode node1 in this.Nodes)
  129. {
  130. text4 = text4 + ((text4 == "") ? "" : "\r\n") + node1.ToXmlString();
  131. }
  132. return string.Format(text3, this.Name, text4);
  133. }
  134. // Properties
  135. public int Depth
  136. {
  137. get{return _Depth;}
  138. set{ _Depth=value;}
  139. }
  140. public string Name
  141. {
  142. get{return _Name;}
  143. set{_Name=value;}
  144. }
  145. public List<rsXmlNode> Nodes
  146. {
  147. get {return _Nodes;}
  148. }
  149. public string Value
  150. {
  151. get{return _Value;}
  152. set{_Value=value;}
  153. }
  154. public static string FormatCodeToXML(string pcString)
  155. {
  156. return pcString.Replace("<", ";lt;").Replace(">", ";gt;").Replace("&", ";amp;");
  157. }
  158. public static string FormatXMLToCode(string pcString)
  159. {
  160. return pcString.Replace(";amp;", "&").Replace(";lt;", "<").Replace(";gt;", ">");
  161. }
  162. #region ICloneable 成员
  163. public object Clone()
  164. {
  165. rsXmlNode loRetVal = new rsXmlNode(this.Name, this.Value);
  166. foreach (rsXmlNode Node in this.Nodes)
  167. {
  168. loRetVal.AddChild((rsXmlNode)Node.Clone());
  169. }
  170. return loRetVal;
  171. }
  172. #endregion
  173. public static rsXmlNode ParseGenericXml(string pcGenericXml)
  174. {
  175. rsXmlNode node1 = null;
  176. StringReader reader1 = new StringReader(pcGenericXml);
  177. XmlTextReader reader2 = new XmlTextReader(reader1);
  178. Hashtable hashtable1 = new Hashtable();
  179. try
  180. {
  181. while (reader2.Read())
  182. {
  183. if (reader2.NodeType == XmlNodeType.Element)
  184. {
  185. rsXmlNode node2 = new rsXmlNode(reader2.Name, "");
  186. node2.Depth = reader2.Depth;
  187. if (hashtable1[reader2.Depth] == null)
  188. {
  189. hashtable1.Add(reader2.Depth, node2);
  190. }
  191. else
  192. {
  193. hashtable1[reader2.Depth] = node2;
  194. }
  195. if (reader2.Depth > 0)
  196. {
  197. ((rsXmlNode)hashtable1[reader2.Depth - 1]).AddChild(node2);
  198. }
  199. else
  200. {
  201. node1 = node2;
  202. }
  203. }
  204. if (reader2.NodeType == XmlNodeType.Text)
  205. {
  206. ((rsXmlNode)hashtable1[reader2.Depth - 1]).Value = FormatXMLToCode(reader2.Value);
  207. }
  208. }
  209. }
  210. catch (Exception e)
  211. {
  212. throw new Exception(e.Message, e);
  213. }
  214. return node1;
  215. }
  216. /// <summary>
  217. /// 给节点重新赋值,如果没有发现该节点则创建该节点
  218. /// </summary>
  219. /// <param name="pcNodeName"></param>
  220. /// <param name="pcValue"></param>
  221. public void SetChildValue(string pcNodeName, string pcValue)
  222. {
  223. SetChildValue(pcNodeName, pcValue, true);
  224. }
  225. /// <summary>
  226. /// 给节点重新赋值
  227. /// </summary>
  228. /// <param name="pcNodeName">节点名称</param>
  229. /// <param name="pcValue">节点值</param>
  230. /// <param name="pbAddNewIfNull">当节点为null的时候,是否创建该节点</param>
  231. public void SetChildValue(string pcNodeName, string pcValue, bool pbAddNewIfNull)
  232. {
  233. rsXmlNode loNode = this.GetChildNode(pcNodeName);
  234. if (loNode == null)
  235. {
  236. if (pbAddNewIfNull)
  237. {
  238. loNode = new rsXmlNode(pcNodeName, pcValue);
  239. this.AddChild(loNode);
  240. }
  241. }
  242. else
  243. {
  244. loNode.Value = pcValue;
  245. }
  246. }
  247. /// <summary>
  248. /// 将该xmlNode 导成 xml字符串形式,包括xml声明和所有的注释信息
  249. /// </summary>
  250. /// <returns></returns>
  251. public string ToAllXmlString()
  252. {
  253. string lcRetVal = ToAllXmlString(0);
  254. if (_XmlDeclaration.Trim().Length > 0)
  255. lcRetVal = _XmlDeclaration + "\r\n" + lcRetVal;
  256. return lcRetVal;
  257. }
  258. private string ToAllXmlString(int piDepth)
  259. {
  260. string lcRetVal = "";
  261. if (this.Nodes.Count == 0)
  262. {
  263. string lcText = "{2}<{0}>{1}</{0}>";
  264. lcRetVal = string.Format(lcText, this.Name, Utils.FormatCodeToXML(this.Value), UtilStr.Replicate(" ", piDepth));
  265. }
  266. else
  267. {
  268. string text3 = "{2}<{0}>\r\n{1}\r\n{2}</{0}>";
  269. string text4 = "";
  270. foreach (rsXmlNode node1 in this.Nodes)
  271. {
  272. text4 += ((text4 == "") ? "" : "\r\n") + node1.ToAllXmlString(piDepth + 1);
  273. }
  274. lcRetVal = string.Format(text3, this.Name, text4, UtilStr.Replicate(" ", piDepth));
  275. }
  276. if (_Comment.Trim().Length > 0)
  277. {
  278. lcRetVal = UtilStr.Replicate(" ", piDepth) + _Comment + "\r\n" + lcRetVal;
  279. }
  280. return lcRetVal;
  281. }
  282. }
  283. }