Parcourir la source

😁增加xml通用序列化方法及其他优化

zuohuaijun il y a 3 ans
Parent
commit
1a0ddf3e80

+ 2 - 1
Admin.NET/Admin.NET.Application/Configuration/App.json

@@ -24,7 +24,8 @@
     },
     "LocalizationSettings": {
         "SupportedCultures": [ "zh-CN", "en-US" ], // 语言列表
-        "DefaultCulture": "zh-CN" // 默认语言
+        "DefaultCulture": "zh-CN", // 默认语言
+        "DateTimeFormatCulture": "zh-CN" // 固定时间区域为特定时区(多语言)
     },
     "CorsAccessorSettings": {
         "WithExposedHeaders": [ "Content-Disposition", "X-Pagination", "access-token", "x-access-token" ], // 如果前端不代理且是axios请求

+ 1 - 1
Admin.NET/Admin.NET.Core/Entity/SysLogVis.cs

@@ -91,7 +91,7 @@ public class SysLogVis : EntityTenant
     /// 日志级别
     /// </summary>
     [SugarColumn(ColumnDescription = "日志级别")]
-    public LogLevel LogLevel { get; set; }
+    public LogLevel? LogLevel { get; set; }
 
     /// <summary>
     /// 账号

+ 48 - 1
Admin.NET/Admin.NET.Core/Util/CommonUtil.cs

@@ -1,4 +1,8 @@
-namespace Admin.NET.Core;
+using System.Xml;
+using System.Xml.Linq;
+using System.Xml.Serialization;
+
+namespace Admin.NET.Core;
 
 /// <summary>
 /// 通用工具类
@@ -34,4 +38,47 @@ public static class CommonUtil
     {
         return $"{App.HttpContext.Request.Scheme}://{App.HttpContext.Request.Host.Value}";
     }
+
+    /// <summary>
+    /// XML序列化
+    /// </summary>
+    /// <typeparam name="T"></typeparam>
+    /// <param name="obj"></param>
+    /// <returns></returns>
+    public static string XmlSerialize<T>(T obj)
+    {
+        if (obj == null) return "";
+
+        var xs = new XmlSerializer(obj.GetType());
+        var stream = new MemoryStream();
+        var setting = new XmlWriterSettings
+        {
+            Encoding = new UTF8Encoding(false),
+            Indent = true
+        };
+        using (var writer = XmlWriter.Create(stream, setting))
+        {
+            var ns = new XmlSerializerNamespaces();
+            ns.Add("", "");
+            xs.Serialize(writer, obj, ns);
+        }
+        return Encoding.UTF8.GetString(stream.ToArray());
+    }
+
+    /// <summary>
+    /// 字符串转XML格式
+    /// </summary>
+    /// <param name="xmlStr"></param>
+    /// <returns></returns>
+    public static XElement XmlParse(string xmlStr)
+    {
+        try
+        {
+            return XElement.Parse(xmlStr);
+        }
+        catch
+        {
+            return null;
+        }
+    }
 }