AnalysisXML.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Xml;
  7. using Newtonsoft.Json;
  8. using Newtonsoft.Json.Linq;
  9. namespace PTMedicalInsurance
  10. {
  11. class AnalysisXML
  12. {
  13. /// <summary>
  14. /// XML转换JSON对象
  15. /// </summary>
  16. /// <param name="sXML"></param>
  17. /// <param name="outParam"></param>
  18. /// <returns></returns>
  19. public JObject ConvertXMLToObject(string sXML)
  20. {
  21. XmlDocument xmlDoc = new XmlDocument();
  22. xmlDoc.LoadXml(sXML);
  23. string jsonStr = JsonConvert.SerializeXmlNode(xmlDoc);
  24. jsonStr = jsonStr.Replace("@", "").ToString();
  25. //MessageBox.Show(jsonStr);
  26. return JObject.Parse(jsonStr);
  27. }
  28. /// <summary>
  29. /// 判断XML是否存在元素
  30. /// </summary>
  31. /// <param name="xmldoc"></param>
  32. /// <param name="xmlnode"></param>
  33. /// <param name="element"></param>
  34. /// <param name="Type">0字符串 1金额</param>
  35. /// <returns></returns>
  36. public string FormatXMLElement(XmlDocument xmldoc, XmlNode xmlnode, string element, int Type)
  37. {
  38. if ((Type != 0) && (Type != 1))
  39. {
  40. Type = 0;
  41. }
  42. string key = "";
  43. key = "//" + element;
  44. if (xmldoc.SelectSingleNode(key) != null)
  45. {
  46. return xmlnode.SelectNodes(element)[0].InnerText;
  47. }
  48. else
  49. {
  50. if (Type == 0)
  51. return "";
  52. else
  53. return "0.00";
  54. }
  55. }
  56. public XmlDocument GetXmlDoc(string sXML)
  57. {
  58. XmlDocument xmlDoc = new XmlDocument();
  59. xmlDoc.LoadXml(sXML);
  60. return xmlDoc;
  61. }
  62. public XmlNode GetNodeFromPath(XmlNode oParentNode, string sPath)
  63. {
  64. XmlNode tmpNode = oParentNode.SelectNodes(sPath)[0];
  65. return tmpNode;
  66. }
  67. public bool CheckState(XmlDocument xmlDoc,out string OutMsg)
  68. {
  69. OutMsg = "";
  70. string sState = GetNodeFromPath(xmlDoc.DocumentElement, "state").Attributes["success"].InnerText;
  71. bool bRet = false;
  72. if (sState.Equals("true"))
  73. {
  74. bRet = true;
  75. //AddLog("调用返回状态:成功");
  76. }
  77. else
  78. {
  79. bRet = false;
  80. //AddLog("调用返回状态:失败");
  81. //读取错误信息
  82. XmlNodeList errNodes = GetNodeFromPath(xmlDoc.DocumentElement, "state").SelectNodes("error");
  83. string sErrNO = "", sErrMsg = "";
  84. for (int i = 0; i < errNodes.Count; i++)
  85. {
  86. if (errNodes[i].Attributes.Count > 0)
  87. {
  88. sErrNO = errNodes[i].Attributes["no"].InnerText;
  89. sErrMsg = errNodes[i].Attributes["info"].InnerText;
  90. //AddLog("调用返回错误:编号 [" + sErrNO + "] -- 描述 [" + sErrMsg + "]");
  91. }
  92. }
  93. //读取警告信息
  94. string sWarNO = "", sWarMsg = "";
  95. XmlNodeList warNodes = GetNodeFromPath(xmlDoc.DocumentElement, "state").SelectNodes("warning");
  96. for (int i = 0; i < warNodes.Count; i++)
  97. {
  98. if (warNodes[i].Attributes.Count > 0)
  99. {
  100. sWarNO = warNodes[i].Attributes["no"].InnerText;
  101. sWarMsg = warNodes[i].Attributes["info"].InnerText;
  102. //AddLog("调用返回警告:编号 [" + sWarNO + "] -- 描述 [" + sWarMsg + "]");
  103. }
  104. }
  105. if ((sWarNO != "") || (sWarMsg != "")|| (sErrNO != "") || (sErrMsg != ""))
  106. {
  107. OutMsg = "错误编号:" + sErrNO + "\n" +
  108. "错误信息:" + sErrMsg + "\n" +
  109. "警告编号:" + sWarNO + "\n" +
  110. "警告信息:" + sWarMsg + "\n";
  111. }
  112. }
  113. //读取信息
  114. //XmlNodeList infNodes = GetNodeFromPath(xmlDoc.DocumentElement, "state").SelectNodes("information");
  115. //for (int i = 0; i < infNodes.Count; i++)
  116. //{
  117. // if (infNodes[i].Attributes.Count > 0)
  118. // {
  119. // string sInfNO = infNodes[i].Attributes["no"].InnerText;
  120. // string sInfMsg = infNodes[i].Attributes["info"].InnerText;
  121. // //AddLog("调用返回信息:编号 [" + sInfNO + "] -- 描述 [" + sInfMsg + "]");
  122. // }
  123. //}
  124. return bRet;
  125. }
  126. //public string bSucess(XmlDocument xmlDoc, string ItemName)
  127. //{
  128. // if (Node(ItemName) <> null)
  129. //}
  130. //public string GetXMLValue(string aXmlText, string FieldName)
  131. //{
  132. //}
  133. public bool CheckOutputState(XmlDocument xmlDoc)
  134. {
  135. string sState = GetNodeFromPath(xmlDoc.DocumentElement, "state").Attributes["success"].InnerText;
  136. bool bRet = false;
  137. if (sState.Equals("true"))
  138. {
  139. bRet = true;
  140. //AddLog("调用返回状态:成功");
  141. }
  142. else
  143. {
  144. bRet = false;
  145. //AddLog("调用返回状态:失败");
  146. }
  147. //读取错误信息
  148. XmlNodeList errNodes = GetNodeFromPath(xmlDoc.DocumentElement, "state").SelectNodes("error");
  149. for (int i = 0; i < errNodes.Count; i++)
  150. {
  151. if (errNodes[i].Attributes.Count > 0)
  152. {
  153. string sErrNO = errNodes[i].Attributes["no"].InnerText;
  154. string sErrMsg = errNodes[i].Attributes["info"].InnerText;
  155. //AddLog("调用返回错误:编号 [" + sErrNO + "] -- 描述 [" + sErrMsg + "]");
  156. }
  157. }
  158. //读取警告信息
  159. XmlNodeList warNodes = GetNodeFromPath(xmlDoc.DocumentElement, "state").SelectNodes("warning");
  160. for (int i = 0; i < warNodes.Count; i++)
  161. {
  162. if (warNodes[i].Attributes.Count > 0)
  163. {
  164. string sWarNO = warNodes[i].Attributes["no"].InnerText;
  165. string sWarMsg = warNodes[i].Attributes["info"].InnerText;
  166. //AddLog("调用返回警告:编号 [" + sWarNO + "] -- 描述 [" + sWarMsg + "]");
  167. }
  168. }
  169. //读取信息
  170. XmlNodeList infNodes = GetNodeFromPath(xmlDoc.DocumentElement, "state").SelectNodes("information");
  171. for (int i = 0; i < infNodes.Count; i++)
  172. {
  173. if (infNodes[i].Attributes.Count > 0)
  174. {
  175. string sInfNO = infNodes[i].Attributes["no"].InnerText;
  176. string sInfMsg = infNodes[i].Attributes["info"].InnerText;
  177. //AddLog("调用返回信息:编号 [" + sInfNO + "] -- 描述 [" + sInfMsg + "]");
  178. }
  179. }
  180. return bRet;
  181. }
  182. }
  183. }