mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2025-07-17 19:37:35 +08:00
refactor(work)
This commit is contained in:
parent
2bb08b2c02
commit
5ca9a48830
@ -1,7 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text;
|
|
||||||
using System.Text.RegularExpressions;
|
|
||||||
using System.Xml.Linq;
|
using System.Xml.Linq;
|
||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
|
|
||||||
@ -209,18 +207,7 @@ namespace SKIT.FlurlHttpClient.Wechat.Work
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using var stream = new MemoryStream();
|
xml = Utilities.XmlUtility.Serialize(callbackModel);
|
||||||
using var writer = new System.Xml.XmlTextWriter(stream, Encoding.UTF8);
|
|
||||||
writer.Formatting = System.Xml.Formatting.None;
|
|
||||||
|
|
||||||
XmlSerializer xmlSerializer = new XmlSerializer(typeof(TEvent), new XmlRootAttribute("xml"));
|
|
||||||
XmlSerializerNamespaces xmlNamespace = new XmlSerializerNamespaces();
|
|
||||||
xmlNamespace.Add(string.Empty, string.Empty);
|
|
||||||
xmlSerializer.Serialize(writer, callbackModel, xmlNamespace);
|
|
||||||
writer.Flush();
|
|
||||||
xml = Encoding.UTF8.GetString(stream.ToArray());
|
|
||||||
xml = Regex.Replace(xml, "\\s+<\\w+ (xsi|d2p1):nil=\"true\"[^>]*/>", string.Empty, RegexOptions.IgnoreCase);
|
|
||||||
xml = Regex.Replace(xml, "<\\?xml[^>]*\\?>", string.Empty, RegexOptions.IgnoreCase);
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
@ -0,0 +1,44 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using System.Xml;
|
||||||
|
using System.Xml.Serialization;
|
||||||
|
|
||||||
|
namespace SKIT.FlurlHttpClient.Wechat.Work.Utilities
|
||||||
|
{
|
||||||
|
internal static class XmlUtility
|
||||||
|
{
|
||||||
|
public static string Serialize(Type type, object obj)
|
||||||
|
{
|
||||||
|
string xml;
|
||||||
|
|
||||||
|
var settings = new XmlWriterSettings();
|
||||||
|
settings.Encoding = Encoding.UTF8;
|
||||||
|
settings.Indent = false;
|
||||||
|
settings.OmitXmlDeclaration = true;
|
||||||
|
settings.WriteEndDocumentOnClose = false;
|
||||||
|
settings.NamespaceHandling = NamespaceHandling.OmitDuplicates;
|
||||||
|
|
||||||
|
using var stream = new MemoryStream();
|
||||||
|
using var writer = XmlWriter.Create(stream, settings);
|
||||||
|
|
||||||
|
XmlSerializer xmlSerializer = new XmlSerializer(type, new XmlRootAttribute("xml"));
|
||||||
|
XmlSerializerNamespaces xmlNamespace = new XmlSerializerNamespaces();
|
||||||
|
xmlNamespace.Add(string.Empty, string.Empty);
|
||||||
|
xmlSerializer.Serialize(writer, obj, xmlNamespace);
|
||||||
|
writer.Flush();
|
||||||
|
xml = Encoding.UTF8.GetString(stream.ToArray());
|
||||||
|
xml = Regex.Replace(xml, "\\s*<\\w+ (xsi|d2p1):nil=\"true\"[^>]*/>", string.Empty, RegexOptions.IgnoreCase);
|
||||||
|
xml = Regex.Replace(xml, "<\\?xml[^>]*\\?>", string.Empty, RegexOptions.IgnoreCase);
|
||||||
|
|
||||||
|
return xml;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string Serialize<T>(T obj)
|
||||||
|
where T : class
|
||||||
|
{
|
||||||
|
return Serialize(typeof(T), obj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user