using System;
|
|
using System.IO;
|
|
using System.IO.Compression;
|
|
using System.Text;
|
|
using System.Xml;
|
|
using System.Xml.Serialization;
|
|
using XmlDocument = BWP.ABCClient.Businesses.XmlDocument;
|
|
|
|
namespace BWP.ABCClient
|
|
{
|
|
public class MsgUtil
|
|
{
|
|
public class BareXmlTextWriter : XmlTextWriter
|
|
{
|
|
public BareXmlTextWriter(TextWriter writer) : base(writer) {}
|
|
|
|
public override void WriteStartDocument() {}
|
|
|
|
public override void WriteStartDocument(bool bare) {}
|
|
|
|
public override void WriteEndElement()
|
|
{
|
|
WriteFullEndElement();
|
|
}
|
|
}
|
|
|
|
public static string MsgToBareXml(object obj)
|
|
{
|
|
return MsgToBareXml(obj, Formatting.None, char.MinValue);
|
|
}
|
|
|
|
public static string MsgToBareXml(object obj, Formatting formatting, char indentChar)
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
using (var stringWriter = new StringWriter(sb)) {
|
|
using (BareXmlTextWriter writer = new BareXmlTextWriter(stringWriter)) {
|
|
writer.Formatting = formatting;
|
|
writer.IndentChar = indentChar;
|
|
var ns = new XmlSerializerNamespaces();
|
|
ns.Add(string.Empty, string.Empty);
|
|
new XmlSerializer(obj.GetType()).Serialize(writer, obj, ns);
|
|
}
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
|
|
public static T ParseXmlToMsg<T>(string xml)
|
|
{
|
|
using (TextReader reader = new StringReader(xml)) {
|
|
return (T)new XmlSerializer(typeof(T)).Deserialize(reader);
|
|
}
|
|
}
|
|
|
|
public static string GetInnerXml(string xml, string xpath)
|
|
{
|
|
var aMessage = new XmlDocument(xml);
|
|
return aMessage.GetValueNoNull(xpath);
|
|
}
|
|
|
|
public static string Compress(string uncompressedString)
|
|
{
|
|
byte[] byteData = Encoding.GetEncoding("GB18030").GetBytes(uncompressedString);
|
|
using (MemoryStream srcStream = new MemoryStream(byteData)) {
|
|
using (var destStream = new MemoryStream()) {
|
|
using (GZipStream compress = new GZipStream(destStream, CompressionMode.Compress)) {
|
|
srcStream.CopyTo(compress);
|
|
}
|
|
return Convert.ToBase64String(destStream.ToArray());
|
|
}
|
|
}
|
|
}
|
|
|
|
public static string DeCompress(string comppressedString)
|
|
{
|
|
byte[] byteInput = Convert.FromBase64String(comppressedString);
|
|
using (var srcStream = new MemoryStream(byteInput)) {
|
|
using (var destStream = new MemoryStream()) {
|
|
using (GZipStream decompress = new GZipStream(srcStream, CompressionMode.Decompress)) {
|
|
decompress.CopyTo(destStream);
|
|
var resultBytes = destStream.ToArray();
|
|
return Encoding.GetEncoding("GB18030").GetString(resultBytes, 0, resultBytes.Length);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static string[] GetMultiInnerXml(string xml, string xpath)
|
|
{
|
|
var xmlDoc = new System.Xml.XmlDocument();
|
|
xmlDoc.LoadXml(xml);
|
|
var nodes = xmlDoc.SelectNodes(xpath);
|
|
if (nodes == null) {
|
|
return null;
|
|
}
|
|
var result = new string[nodes.Count];
|
|
for (int i = 0; i < result.Length; i++) {
|
|
result[i] = nodes[i].InnerXml;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static string FromBase64StringToXml(string msg)
|
|
{
|
|
byte[] bytes = Convert.FromBase64String(msg);
|
|
return Encoding.GetEncoding("gb2312").GetString(bytes, 0, bytes.Length);
|
|
}
|
|
}
|
|
}
|