XmlHelper.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using PTMedicalInsurance.Variables;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8. using System.Xml;
  9. using System.Xml.Linq;
  10. using System.Xml.XPath;
  11. namespace PTMedicalInsurance.Helper
  12. {
  13. class XmlHelper
  14. {
  15. /// <summary>
  16. /// 组织中心入参
  17. /// </summary>
  18. /// <param name="infno"></param>
  19. /// <param name="input"></param>
  20. /// <returns></returns>
  21. public string setInput(XElement xInput)
  22. {
  23. XDocument XDoc = new XDocument();
  24. XDoc.Add(xInput);
  25. //XDoc.Declaration = new XDeclaration("1.0", "GBK", "yes");
  26. XDoc.Declaration = new XDeclaration("1.0", "UTF-8", "yes");
  27. return XDoc.ToStringWithDeclaration(); //扩展方法 https://learn.microsoft.com/zh-cn/dotnet/csharp/programming-guide/classes-and-structs/extension-methods
  28. }
  29. /// <summary>
  30. /// 根据JSonPath查找节点值,如果节点不存在则返回空值
  31. /// </summary>
  32. /// <param name="jo"></param>
  33. /// <param name="jsonPath"></param>
  34. /// <returns></returns>
  35. public string getDestValue(string source, string destPath)
  36. {
  37. try
  38. {
  39. XDocument XDoc = XDocument.Parse(source);
  40. XElement XDest = XDoc.XPathSelectElement(destPath);
  41. if (XDest != null)
  42. return XDest.Value;
  43. else
  44. return "节点为空!";
  45. }
  46. catch (Exception ex)
  47. {
  48. return "查找XML节点异常:" + ex.Message;
  49. }
  50. }
  51. public string ToJSON(string source)
  52. {
  53. string json = "";
  54. try
  55. {
  56. source = Regex.Replace(source, "<row",
  57. "<row xmlns:json=\"http://james.newtonking.com/projects/json\" json:Array=\"true\" ", RegexOptions.IgnoreCase);
  58. XDocument xDoc = XDocument.Parse(source);
  59. json = Newtonsoft.Json.JsonConvert.SerializeXmlNode(xDoc.ToXmlDocument());
  60. return json;
  61. }
  62. catch (Exception ex)
  63. {
  64. return "转换异常:" + ex.Message;
  65. }
  66. finally
  67. {
  68. Global.writeLog("XMLToJSON", source,json);
  69. }
  70. }
  71. public string ToXML(string jsonSource)
  72. {
  73. try
  74. {
  75. XmlDocument xe = Newtonsoft.Json.JsonConvert.DeserializeXmlNode(jsonSource);
  76. return xe.OuterXml;
  77. }
  78. catch (Exception ex)
  79. {
  80. return "转换异常:" + ex.Message;
  81. }
  82. }
  83. }
  84. public static class MyExtensions
  85. {
  86. /// <summary>
  87. /// Doc输出无声明,强行补上
  88. /// </summary>
  89. /// <param name="doc"></param>
  90. /// <returns></returns>
  91. public static string ToStringWithDeclaration(this XDocument doc)
  92. {
  93. return doc.Declaration.ToString() + Environment.NewLine + doc.ToString();
  94. }
  95. /// <summary>
  96. /// 无缩进格式(压缩格式)
  97. /// </summary>
  98. /// <param name="doc"></param>
  99. /// <returns></returns>
  100. public static string ToCompressString(this XDocument doc)
  101. {
  102. return doc.Declaration.ToString() + doc.ToString(SaveOptions.DisableFormatting);
  103. }
  104. public static XmlDocument ToXmlDocument(this XDocument xDocument)
  105. {
  106. var xmlDocument = new XmlDocument();
  107. using (var xmlReader = xDocument.CreateReader())
  108. {
  109. xmlDocument.Load(xmlReader);
  110. }
  111. return xmlDocument;
  112. }
  113. public static XDocument ToXDocument(this XmlDocument xmlDocument)
  114. {
  115. using (var nodeReader = new XmlNodeReader(xmlDocument))
  116. {
  117. nodeReader.MoveToContent();
  118. return XDocument.Load(nodeReader);
  119. }
  120. }
  121. }
  122. }