From 8cb24bc6afef66f4f13705ae02c5663ebb1ee085 Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Tue, 29 Mar 2022 16:01:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Exml=E5=BA=8F=E5=88=97?= =?UTF-8?q?=E5=8C=96=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Falcon.SugarApi/IServiceCollectionExtend.cs | 10 ++ Falcon.SugarApi/XmlSerialize/IXmlSerialize.cs | 25 +++++ Falcon.SugarApi/XmlSerialize/MyXmlWriter.cs | 104 ++++++++++++++++++ Falcon.SugarApi/XmlSerialize/XmlSerialize.cs | 75 +++++++++++++ .../XmlSerialize/XmlSerializeFactory.cs | 28 +++++ .../XmlSerialize/XmlSerializeSettings.cs | 37 +++++++ 6 files changed, 279 insertions(+) create mode 100644 Falcon.SugarApi/XmlSerialize/IXmlSerialize.cs create mode 100644 Falcon.SugarApi/XmlSerialize/MyXmlWriter.cs create mode 100644 Falcon.SugarApi/XmlSerialize/XmlSerialize.cs create mode 100644 Falcon.SugarApi/XmlSerialize/XmlSerializeFactory.cs create mode 100644 Falcon.SugarApi/XmlSerialize/XmlSerializeSettings.cs diff --git a/Falcon.SugarApi/IServiceCollectionExtend.cs b/Falcon.SugarApi/IServiceCollectionExtend.cs index f935323..20e9741 100644 --- a/Falcon.SugarApi/IServiceCollectionExtend.cs +++ b/Falcon.SugarApi/IServiceCollectionExtend.cs @@ -1,5 +1,6 @@ using Falcon.SugarApi.ApiDefinistions; using Falcon.SugarApi.DatabaseDefinitions; +using Falcon.SugarApi.XmlSerialize; using Microsoft.AspNetCore.Mvc.ApplicationModels; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; @@ -50,5 +51,14 @@ namespace Falcon.SugarApi services.TryAddEnumerable(ServiceDescriptor.Transient()); return services; } + + /// + /// 添加xml序列化工厂XmlSerializeFactory,该工厂可以创建一个IXmlSerialize实现 + /// + /// 服务集合 + /// 服务集合 + public static IServiceCollection AddXmlSerializeFactory(this IServiceCollection services) { + return services.AddSingleton(new XmlSerializeFactory()); + } } } diff --git a/Falcon.SugarApi/XmlSerialize/IXmlSerialize.cs b/Falcon.SugarApi/XmlSerialize/IXmlSerialize.cs new file mode 100644 index 0000000..93b5a9c --- /dev/null +++ b/Falcon.SugarApi/XmlSerialize/IXmlSerialize.cs @@ -0,0 +1,25 @@ +namespace Falcon.SugarApi.XmlSerialize +{ + /// + /// XML文档序列化接口 + /// + public interface IXmlSerialize + { + /// + /// 序列化对象 + /// + /// 对象类型 + /// 对象 + /// 序列化字符串 + public string Serialize(T obj); + + /// + /// 反序列化对象 + /// + /// 对象类型 + /// 序列化字符串 + /// 对象实例 + public T? Deserialize(string str) where T : class; + } + +} diff --git a/Falcon.SugarApi/XmlSerialize/MyXmlWriter.cs b/Falcon.SugarApi/XmlSerialize/MyXmlWriter.cs new file mode 100644 index 0000000..5998b88 --- /dev/null +++ b/Falcon.SugarApi/XmlSerialize/MyXmlWriter.cs @@ -0,0 +1,104 @@ +using System; +using System.Xml; + +namespace Falcon.SugarApi.XmlSerialize +{ + /// + /// XmlWriter扩展。实现完整的扩展标记 + /// + /// 接口要求书写完整的结束标记,但是默认XmlWriter使用/结束空标记 + public class MyXmlWriter : XmlWriter, IDisposable + { + /// + /// 基础XmlWriter + /// + public XmlWriter BaseWriter { get; set; } + /// + /// 序列化设置 + /// + public XmlSerializeSettings MySettings { get; set; } + /// + /// 通过基础序列化器和序列化设置实例化 + /// + /// 基础序列化器 + /// 序列化设置 + public MyXmlWriter(XmlWriter writer, XmlSerializeSettings settings) { + this.BaseWriter = writer; + this.MySettings = settings; + } + +#pragma warning disable CS1591 // 缺少对公共可见类型或成员的 XML 注释 + #region XmlWriter实现 + public override WriteState WriteState => BaseWriter.WriteState; + + public override void Flush() => this.BaseWriter.Flush(); + + public override string? LookupPrefix(string ns) => BaseWriter.LookupPrefix(ns); + + public override void WriteBase64(byte[] buffer, int index, int count) => this.BaseWriter.WriteBase64(buffer, index, count); + + public override void WriteCData(string? text) => this.BaseWriter.WriteCData(text); + + public override void WriteCharEntity(char ch) => this.BaseWriter.WriteCharEntity(ch); + + public override void WriteChars(char[] buffer, int index, int count) => this.BaseWriter.WriteChars(buffer, index, count); + + public override void WriteComment(string? text) => this.BaseWriter.WriteComment(text); + + public override void WriteDocType(string name, string? pubid, string? sysid, string? subset) => this.BaseWriter.WriteDocType(name, pubid, sysid, subset); + + public override void WriteEndAttribute() => this.BaseWriter.WriteEndAttribute(); + + /// + /// 写入完整的关闭节点 + /// + public override void WriteEndElement() { + if (this.MySettings.WriteFullEndElement) { + BaseWriter.WriteFullEndElement(); + } + else { + BaseWriter.WriteEndElement(); + } + } + + public override void WriteFullEndElement() => BaseWriter.WriteFullEndElement(); + + public override void Close() => BaseWriter.Close(); + + public override void WriteEndDocument() => BaseWriter.WriteEndDocument(); + + public override void WriteEntityRef(string name) => BaseWriter.WriteEntityRef(name); + + public override void WriteProcessingInstruction(string name, string? text) => BaseWriter.WriteProcessingInstruction(name, text); + + public override void WriteRaw(string data) => BaseWriter.WriteRaw(data); + + public override void WriteRaw(char[] buffer, int index, int count) => BaseWriter.WriteRaw(buffer, index, count); + + public override void WriteStartAttribute(string? prefix, string localName, string? ns) => BaseWriter.WriteStartAttribute(prefix, localName, ns); + + public override void WriteStartDocument(bool standalone) => BaseWriter.WriteStartDocument(standalone); + + public override void WriteStartDocument() => BaseWriter.WriteStartDocument(); + + public override void WriteStartElement(string? prefix, string localName, string? ns) => BaseWriter.WriteStartElement(prefix, localName, ns); + + public override void WriteString(string? text) => BaseWriter.WriteString(text); + + public override void WriteSurrogateCharEntity(char lowChar, char highChar) => BaseWriter.WriteSurrogateCharEntity(lowChar, highChar); + + public override void WriteWhitespace(string? ws) => BaseWriter.WriteWhitespace(ws); + + #endregion +#pragma warning restore CS1591 // 缺少对公共可见类型或成员的 XML 注释 + + protected override void Dispose(bool disposing) { + if (disposing) { + this.BaseWriter.Dispose(); + } + base.Dispose(disposing); + } + + } + +} diff --git a/Falcon.SugarApi/XmlSerialize/XmlSerialize.cs b/Falcon.SugarApi/XmlSerialize/XmlSerialize.cs new file mode 100644 index 0000000..818de9b --- /dev/null +++ b/Falcon.SugarApi/XmlSerialize/XmlSerialize.cs @@ -0,0 +1,75 @@ +using System.IO; +using System.Text; +using System.Xml; +using System.Xml.Serialization; + +namespace Falcon.SugarApi.XmlSerialize +{ + /// + /// XML序列化实现 + /// + public class XmlSerialize : IXmlSerialize + { + /// + /// 序列化设置 + /// + public XmlSerializeSettings Settings { get; set; } + + /// + /// 使用默认配置的序列化器 + /// + public XmlSerialize() : this(new XmlSerializeSettings()) { } + /// + /// 使用特定配置的序列化器 + /// + /// 序列化配置 + public XmlSerialize(XmlSerializeSettings settings) { + this.Settings = settings; + } + + /// + /// 反序列化对象 + /// + /// 对象类型 + /// 序列化字符串 + /// 对象实例 + public T? Deserialize(string str) where T : class { + var serializer = new XmlSerializer(typeof(T)); + using var ms = new MemoryStream(Encoding.UTF8.GetBytes(str)); + var obj = serializer.Deserialize(ms) as T; + return obj; + } + + /// + /// 序列化对象 + /// + /// 对象类型 + /// 对象 + /// 序列化字符串 + public string Serialize(T obj) { + var xmlSerializer = new XmlSerializer(typeof(T)); + using var ms = new MemoryStream(); + XmlSerializerNamespaces xmlSerializerNamespaces = new(); + if (this.Settings.Namespaces.Count > 0) { + foreach (var item in this.Settings.Namespaces) { + xmlSerializerNamespaces.Add(item.Key, item.Value); + } + } + else { + xmlSerializerNamespaces.Add("", ""); + } + var settings = new XmlWriterSettings { + OmitXmlDeclaration = this.Settings.OmitXmlDeclaration, + Encoding =this.Settings.Encoding, + Indent = this.Settings.Indent, + IndentChars = this.Settings.IndentChars, + }; + using XmlWriter xmlWriter = new MyXmlWriter(XmlWriter.Create(ms, settings),this.Settings); + xmlSerializer.Serialize(xmlWriter, obj, xmlSerializerNamespaces); + ms.Position = 0; + using var sr = new StreamReader(ms, Encoding.UTF8); + return sr.ReadToEnd(); + } + } + +} diff --git a/Falcon.SugarApi/XmlSerialize/XmlSerializeFactory.cs b/Falcon.SugarApi/XmlSerialize/XmlSerializeFactory.cs new file mode 100644 index 0000000..abbf2a4 --- /dev/null +++ b/Falcon.SugarApi/XmlSerialize/XmlSerializeFactory.cs @@ -0,0 +1,28 @@ +using System; + +namespace Falcon.SugarApi.XmlSerialize +{ + /// + /// xml序列化器工厂 + /// + public class XmlSerializeFactory + { + /// + /// 使用默认设置创建xml序列化器 + /// + /// 序列化器 + public IXmlSerialize CreateXmlSerialize() { + return new XmlSerialize(); + } + /// + /// 使用特定设置创建xml序列化器 + /// + /// 特定设置 + /// 序列化器 + public IXmlSerialize CreateXmlSerialize(Action setter) { + var settings = new XmlSerializeSettings { }; + setter(settings); + return new XmlSerialize(settings); + } + } +} diff --git a/Falcon.SugarApi/XmlSerialize/XmlSerializeSettings.cs b/Falcon.SugarApi/XmlSerialize/XmlSerializeSettings.cs new file mode 100644 index 0000000..b0028a5 --- /dev/null +++ b/Falcon.SugarApi/XmlSerialize/XmlSerializeSettings.cs @@ -0,0 +1,37 @@ +using System.Collections.Generic; +using System.Text; + +namespace Falcon.SugarApi.XmlSerialize +{ + /// + /// XML序列化设置 + /// + public class XmlSerializeSettings + { + /// + /// 编写XML声明。默认false + /// + public bool OmitXmlDeclaration { set; get; } = false; + /// + /// XML编码格式。默认Encoding.UTF8 + /// + public Encoding Encoding { set; get; } = Encoding.UTF8; + /// + /// 是否换行缩进。默认true + /// + public bool Indent { set; get; } = true; + /// + /// 缩进使用字符串。默认" " + /// + public string IndentChars { set; get; } = " "; + /// + /// XML名字空间。默认空 + /// + public List> Namespaces { get; set; } = new(); + /// + /// 对空节点使用完整的结束标记。默认true + /// + public bool WriteFullEndElement { get; set; } = true; + } + +}