IwbXmlNode.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Xml;
  5. using IwbZero.ToolCommon.LogHelpers;
  6. using IwbZero.ToolCommon.StringModel;
  7. namespace IwbZero.IwbBase
  8. {
  9. public class IwbXmlNode
  10. {
  11. private int Id { get; }
  12. private string _value;
  13. public IwbXmlNode(string pcName, string pcValue, int depth = -1)
  14. {
  15. Name = pcName;
  16. Depth = depth;
  17. _value = pcValue.FormatXml2Code();
  18. Id = StringHelper.GetRandom().ValI();
  19. }
  20. public void AddChild(IwbXmlNode poNode)
  21. {
  22. Nodes.Add(poNode);
  23. }
  24. public override bool Equals(object obj)
  25. {
  26. return obj != null && (GetHashCode() == obj.GetHashCode());
  27. }
  28. public IwbXmlNode GetChildNode(string pcNodeName)
  29. {
  30. string pcMessage = "";
  31. return GetChildNode(pcNodeName, ref pcMessage, false);
  32. }
  33. public IwbXmlNode GetChildNode(string pcNodeName, ref string pcMessage, bool plMandatory)
  34. {
  35. IwbXmlNode node = null;
  36. foreach (IwbXmlNode node2 in Nodes)
  37. {
  38. if (node2.Name.UAndT() == pcNodeName.UAndT())
  39. {
  40. node = node2;
  41. break;
  42. }
  43. }
  44. if ((node == null) && plMandatory)
  45. {
  46. pcMessage = pcMessage + ((pcMessage == "") ? "" : "\r\n") + pcNodeName;
  47. }
  48. return node;
  49. }
  50. public string GetChildValue(string pcNodeName)
  51. {
  52. string str = "";
  53. IwbXmlNode childNode = GetChildNode(pcNodeName);
  54. if (childNode != null)
  55. {
  56. str = childNode.Value.Trim();
  57. }
  58. return str;
  59. }
  60. public override int GetHashCode()
  61. {
  62. return Id.GetHashCode();
  63. }
  64. public override string ToString()
  65. {
  66. string name = Name;
  67. if (Value != "")
  68. {
  69. name = name + "=" + Value;
  70. }
  71. return name;
  72. }
  73. public string ToXmlString()
  74. {
  75. if (Nodes.Count == 0)
  76. {
  77. string str2 = "<{0}>{1}</{0}>";
  78. return string.Format(str2, Name, Value.FormatXml2Code());
  79. }
  80. string format = "<{0}>\r\n{1}\r\n</{0}>";
  81. string str4 = "";
  82. foreach (IwbXmlNode node in Nodes)
  83. {
  84. str4 = str4 + ((str4 == "") ? "" : "\r\n") + node.ToXmlString();
  85. }
  86. return string.Format(format, Name, str4);
  87. }
  88. public int Depth { get; set; }
  89. public string Name { get; set; }
  90. public ArrayList Nodes { get; } = new ArrayList();
  91. public static IwbXmlNode ParseGenericXml(string pcGenericXml)
  92. {
  93. IwbXmlNode node = null;
  94. StringReader input = new StringReader(pcGenericXml);
  95. XmlTextReader reader2 = new XmlTextReader(input);
  96. Hashtable hashtable = new Hashtable();
  97. try
  98. {
  99. while (reader2.Read())
  100. {
  101. if (reader2.NodeType == XmlNodeType.Element)
  102. {
  103. IwbXmlNode node2 = new IwbXmlNode(reader2.Name, "")
  104. {
  105. Depth = reader2.Depth
  106. };
  107. if (hashtable[reader2.Depth] == null)
  108. {
  109. hashtable.Add(reader2.Depth, node2);
  110. }
  111. else
  112. {
  113. hashtable[reader2.Depth] = node2;
  114. }
  115. if (reader2.Depth > 0)
  116. {
  117. ((IwbXmlNode)hashtable[reader2.Depth - 1]).AddChild(node2);
  118. }
  119. else
  120. {
  121. node = node2;
  122. }
  123. }
  124. if (reader2.NodeType == XmlNodeType.Text)
  125. {
  126. ((IwbXmlNode)hashtable[reader2.Depth - 1]).Value = reader2.Value.FormatXml2Code();
  127. }
  128. }
  129. }
  130. catch (Exception e)
  131. {
  132. typeof(IwbXmlNode).LogError(e);
  133. node = null;
  134. }
  135. return node;
  136. }
  137. public string Value
  138. {
  139. get => _value;
  140. set => _value = value.FormatXml2Code();
  141. }
  142. }
  143. }