Xmlconfig.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. using System;
  2. using System.Xml;
  3. using WHC.OrderWater.Commons;
  4. public class Xmlconfig
  5. {
  6. public static bool ReadXmlnode(string str,string node,string value,out object result)
  7. {
  8. result = "";
  9. try
  10. {
  11. XmlDocument xml = new XmlDocument();
  12. //xml.Load(path);
  13. xml.LoadXml(str);
  14. var b = xml.SelectSingleNode(node);
  15. result = xml.SelectSingleNode(node).SelectSingleNode(value).InnerText;
  16. return true;
  17. }
  18. catch(Exception ex)
  19. {
  20. result = ex.Message;
  21. return false;
  22. }
  23. }
  24. public static bool WriteXmlnode(string path, string node, string value, out object result)
  25. {
  26. result = "";
  27. try
  28. {
  29. XmlDocument xml = new XmlDocument();
  30. xml.Load(path);
  31. xml.SelectSingleNode(node).InnerText = value;
  32. return true;
  33. }
  34. catch (Exception ex)
  35. {
  36. result = ex.Message;
  37. return false;
  38. }
  39. }
  40. public static bool KeyCountXml(string str, string node, string node2,out string code,out string desc,out string value)
  41. {
  42. code = "";
  43. value = "";
  44. try
  45. {
  46. XmlDocument xml = new XmlDocument();
  47. //xml.Load(path);
  48. xml.LoadXml(str);
  49. code = xml.SelectSingleNode(node).Attributes.GetNamedItem("code").InnerText;
  50. desc = xml.SelectSingleNode(node).Attributes.GetNamedItem("desc").InnerText;
  51. if(code=="200")
  52. {
  53. value = xml.SelectSingleNode(node + "//" + node2).Attributes.GetNamedItem("value").InnerText;
  54. return true;
  55. }
  56. return false;
  57. }
  58. catch (Exception ex)
  59. {
  60. desc = ex.Message;
  61. return false;
  62. }
  63. }
  64. public static bool AutoKeyStatusXml(string str, string node, out string code, out string desc)
  65. {
  66. code = "";
  67. try
  68. {
  69. XmlDocument xml = new XmlDocument();
  70. //xml.Load(path);
  71. xml.LoadXml(str);
  72. code = xml.SelectSingleNode(node).Attributes.GetNamedItem("code").InnerText;
  73. desc = xml.SelectSingleNode(node).Attributes.GetNamedItem("desc").InnerText;
  74. if (code == "200")
  75. {
  76. return true;
  77. }
  78. return false;
  79. }
  80. catch (Exception ex)
  81. {
  82. desc = ex.Message;
  83. return false;
  84. }
  85. }
  86. public static bool GetDIDKeyXml(string str, string node, string node2, out string code, out string desc, out string value)
  87. {
  88. code = "";
  89. value = "";
  90. try
  91. {
  92. XmlDocument xml = new XmlDocument();
  93. //xml.Load(path);
  94. xml.LoadXml(str);
  95. code = xml.SelectSingleNode(node).Attributes.GetNamedItem("code").InnerText;
  96. desc = xml.SelectSingleNode(node).Attributes.GetNamedItem("desc").InnerText;
  97. if (code == "200")
  98. {
  99. value = xml.SelectSingleNode(node + "//" + node2).Attributes.GetNamedItem("deviceid").InnerText;
  100. string CRC32= xml.SelectSingleNode(node + "//" + node2).Attributes.GetNamedItem("crc32").InnerText;
  101. if (CrcUtils.CRC32(value) ==Convert.ToUInt32(CRC32, 16))
  102. {
  103. return true;
  104. }
  105. else
  106. {
  107. desc = "CRC32 error";
  108. return false;
  109. }
  110. }
  111. return false;
  112. }
  113. catch (Exception ex)
  114. {
  115. desc = ex.Message;
  116. return false;
  117. }
  118. }
  119. public static bool GetOthersKeyXml(string str, string node, string node2, out string code, out string desc, out string value, out string md5)
  120. {
  121. md5 = "";
  122. code = "";
  123. value = "";
  124. try
  125. {
  126. XmlDocument xml = new XmlDocument();
  127. //xml.Load(path);
  128. xml.LoadXml(str);
  129. code = xml.SelectSingleNode(node).Attributes.GetNamedItem("code").InnerText;
  130. desc = xml.SelectSingleNode(node).Attributes.GetNamedItem("desc").InnerText;
  131. if (code == "200")
  132. {
  133. value = xml.SelectSingleNode(node + "//" + node2).Attributes.GetNamedItem("value").InnerText;
  134. if (xml.SelectSingleNode(node + "//" + node2).Attributes.GetNamedItem("md5value") != null)
  135. md5 = xml.SelectSingleNode(node + "//" + node2).Attributes.GetNamedItem("md5value").InnerText;
  136. string CRC32 = xml.SelectSingleNode(node + "//" + node2).Attributes.GetNamedItem("crc32").InnerText;
  137. if (CrcUtils.CRC32(value) == Convert.ToUInt32(CRC32, 16))
  138. {
  139. return true;
  140. }
  141. else
  142. {
  143. desc = "CRC32 error";
  144. return false;
  145. }
  146. }
  147. return false;
  148. }
  149. catch (Exception ex)
  150. {
  151. desc = ex.Message;
  152. return false;
  153. }
  154. }
  155. /// <summary>
  156. /// 解析并获取返回2个key的xml.
  157. /// </summary>
  158. /// <returns></returns>
  159. public static bool Get2KeyInfoFromXml(string str, string dataKey, string toolKey, out string code, out string desc, out string acaskey_data, out string acaskey_datamd5, out string acaskey_tool, out string acaskey_toolmd5)
  160. {
  161. acaskey_data = "";
  162. acaskey_tool = "";
  163. code = "";
  164. acaskey_datamd5 = "";
  165. acaskey_toolmd5 = "";
  166. try
  167. {
  168. XmlDocument xml = new XmlDocument();
  169. xml.LoadXml(str);
  170. code = xml.SelectSingleNode("response").Attributes.GetNamedItem("code").InnerText;
  171. desc = xml.SelectSingleNode("response").Attributes.GetNamedItem("desc").InnerText;
  172. if (code == "200")
  173. {
  174. acaskey_data = xml.SelectSingleNode("response//" + dataKey).Attributes.GetNamedItem("value").InnerText;
  175. if (xml.SelectSingleNode("response//" + dataKey).Attributes.GetNamedItem("md5value") != null)
  176. acaskey_datamd5 = xml.SelectSingleNode("response//" + dataKey).Attributes.GetNamedItem("md5value").InnerText;
  177. bool bCRC321 = false;
  178. string CRC321 = "";
  179. if (xml.SelectSingleNode("response//" + dataKey).Attributes.GetNamedItem("crc32") != null)
  180. {
  181. bCRC321 = true;
  182. CRC321 = xml.SelectSingleNode("response//" + dataKey).Attributes.GetNamedItem("crc32").InnerText;
  183. }
  184. acaskey_tool = xml.SelectSingleNode("response//" + toolKey).Attributes.GetNamedItem("value").InnerText;
  185. if (xml.SelectSingleNode("response//" + toolKey).Attributes.GetNamedItem("md5value") != null)
  186. acaskey_toolmd5 = xml.SelectSingleNode("response//" + toolKey).Attributes.GetNamedItem("md5value").InnerText;
  187. bool bCRC322 = false;
  188. string CRC322 = "";
  189. if (xml.SelectSingleNode("response//" + toolKey).Attributes.GetNamedItem("crc32") != null)
  190. {
  191. bCRC322 = true;
  192. CRC322 = xml.SelectSingleNode("response//" + toolKey).Attributes.GetNamedItem("crc32").InnerText;
  193. }
  194. if (bCRC321)
  195. {
  196. if (CrcUtils.CRC32(acaskey_data) != Convert.ToUInt32(CRC321, 16))
  197. {
  198. desc = "CRC32 error";
  199. return false;
  200. }
  201. }
  202. if (bCRC322)
  203. {
  204. if (CrcUtils.CRC32(acaskey_tool) != Convert.ToUInt32(CRC322, 16))
  205. {
  206. desc = "CRC32 error";
  207. return false;
  208. }
  209. }
  210. }
  211. return true;
  212. }
  213. catch (Exception ex)
  214. {
  215. desc = ex.Message;
  216. return false;
  217. }
  218. }
  219. public static bool GetWidevineAndAttestationKeyXml(string str,
  220. string node,
  221. out string code,
  222. out string desc,
  223. out bool isAndroidTV,
  224. out string WidevineKey,
  225. out string AttestationKey,
  226. out string AttestationMd5)
  227. {
  228. code = "";
  229. isAndroidTV = false;
  230. WidevineKey = "";
  231. AttestationKey = "";
  232. AttestationMd5 = "";
  233. try
  234. {
  235. XmlDocument xml = new XmlDocument();
  236. //xml.Load(path);
  237. xml.LoadXml(str);
  238. code = xml.SelectSingleNode(node).Attributes.GetNamedItem("code").InnerText;
  239. desc = xml.SelectSingleNode(node).Attributes.GetNamedItem("desc").InnerText;
  240. isAndroidTV = !xml.SelectSingleNode(node).Attributes.GetNamedItem("androidtv").InnerText.Equals("1");
  241. if (code == "200")
  242. {
  243. WidevineKey = xml.SelectSingleNode(node + "//widevine").Attributes.GetNamedItem("value").InnerText;
  244. string CRC32 = xml.SelectSingleNode(node + "//widevine").Attributes.GetNamedItem("crc32").InnerText;
  245. if (CrcUtils.CRC32(WidevineKey) != Convert.ToUInt32(CRC32, 16))
  246. {
  247. desc = "widevine CRC32 error";
  248. return false;
  249. }
  250. if (isAndroidTV)
  251. {
  252. #if false
  253. // 判断节点是否存在;
  254. if (xml.GetElementsByTagName("attestation") == null)
  255. {
  256. desc = "isAndroidTV: but attestation node is Null";
  257. return false;
  258. }
  259. #endif
  260. AttestationKey = xml.SelectSingleNode(node + "//attestation").Attributes.GetNamedItem("value").InnerText;
  261. CRC32 = xml.SelectSingleNode(node + "//attestation").Attributes.GetNamedItem("crc32").InnerText;
  262. if (CrcUtils.CRC32(AttestationKey) != Convert.ToUInt32(CRC32, 16))
  263. {
  264. desc = "attestation CRC32 error";
  265. return false;
  266. }
  267. else
  268. {
  269. AttestationMd5 = xml.SelectSingleNode(node + "//attestation").Attributes.GetNamedItem("md5value").InnerText;
  270. }
  271. }
  272. return true;
  273. }
  274. return false;
  275. }
  276. catch (Exception ex)
  277. {
  278. desc = ex.Message;
  279. return false;
  280. }
  281. }
  282. public static bool ReportXml(string str, string node, out string code, out string desc)
  283. {
  284. code = "";
  285. try
  286. {
  287. XmlDocument xml = new XmlDocument();
  288. //xml.Load(path);
  289. xml.LoadXml(str);
  290. code = xml.SelectSingleNode(node).Attributes.GetNamedItem("code").InnerText;
  291. desc = xml.SelectSingleNode(node).Attributes.GetNamedItem("desc").InnerText;
  292. if (code == "200")
  293. {
  294. return true;
  295. }
  296. return false;
  297. }
  298. catch (Exception ex)
  299. {
  300. desc = ex.Message;
  301. return false;
  302. }
  303. }
  304. public static bool GetDSNXml(string str, string node, out string code, out string desc, out string value)
  305. {
  306. code = "";
  307. value = "";
  308. try
  309. {
  310. XmlDocument xml = new XmlDocument();
  311. xml.LoadXml(str);
  312. code = xml.SelectSingleNode(node).Attributes.GetNamedItem("code").InnerText;
  313. desc = xml.SelectSingleNode(node).Attributes.GetNamedItem("desc").InnerText;
  314. if (code == "200")
  315. {
  316. value = xml.SelectSingleNode(node + "//" + "firetvDsn").Attributes.GetNamedItem("dsn").InnerText;
  317. return true;
  318. }
  319. return false;
  320. }
  321. catch (Exception ex)
  322. {
  323. desc = ex.Message;
  324. return false;
  325. }
  326. }
  327. }