public static byte[] DatasetToXML(DataSet DS) { using (MemoryStream memstream = new MemoryStream()) { string InFile = null; string OutFile = null; try { if (ConfigurationManager.AppSettings["CompressDatasets"] == "1") { InFile = Path.GetTempFileName(); OutFile = Path.GetTempFileName(); DS.WriteXml(InFile, XmlWriteMode.WriteSchema); CompressionUtils.CompressFile(InFile, OutFile); using (FileStream filestream = new FileStream(OutFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { try { byte[] buffer = Encoding.Default.GetBytes(CompressionUtils.COMPRESSION_SIGNATURE); memstream.Write(buffer, 0, buffer.Length); buffer = new byte[filestream.Length]; filestream.Read(buffer, 0, buffer.Length); memstream.Write(buffer, 0, buffer.Length); } finally { filestream.Close(); } } } else { DS.WriteXml(memstream, XmlWriteMode.WriteSchema); } } finally { memstream.Close(); try { if (!String.IsNullOrEmpty(InFile)) { File.Delete(InFile); } } catch {} try { if (!String.IsNullOrEmpty(OutFile)) { File.Delete(OutFile); } } catch {} } return(memstream.ToArray()); } }
public static DataSet XMLToDataset(byte[] XML) { string inFile = null, outFile = null; try { DataSet ds = new DataSet(); if (Encoding.Default.GetString(XML, 0, CompressionUtils.COMPRESSION_SIGNATURE.Length) == CompressionUtils.COMPRESSION_SIGNATURE) { inFile = Path.GetTempFileName(); outFile = Path.GetTempFileName(); using (FileStream fs = new FileStream(inFile, FileMode.Open, FileAccess.Write)) { fs.Write( XML, CompressionUtils.COMPRESSION_SIGNATURE.Length, XML.Length - CompressionUtils.COMPRESSION_SIGNATURE.Length ); } CompressionUtils.DecompressFile(inFile, outFile); ds.ReadXml(outFile, XmlReadMode.ReadSchema); } else { using (MemoryStream ms = new MemoryStream(XML)) { ds.ReadXml(ms, XmlReadMode.ReadSchema); } } return(ds); } finally { if (!String.IsNullOrEmpty(inFile) && File.Exists(inFile)) { try { File.Delete(inFile); } catch {} } if (!String.IsNullOrEmpty(outFile) && File.Exists(outFile)) { try { File.Delete(outFile); } catch {} } } }
public static byte[] StringToXML(string s) { byte [] result; if (ConfigurationManager.AppSettings["CompressDatasets"] == "1") { // El sistema está configurado para utilizar compresión string InFile = null; string OutFile = null; try { InFile = Path.GetTempFileName(); OutFile = Path.GetTempFileName(); using (StreamWriter streamwriter = new StreamWriter(InFile)) { try { streamwriter.Write(s); } finally { streamwriter.Close(); } } CompressionUtils.CompressFile(InFile, OutFile); List <byte> list = new List <byte>(); list.AddRange(Encoding.Default.GetBytes(CompressionUtils.COMPRESSION_SIGNATURE)); using (FileStream filestream = new FileStream(OutFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { try { byte[] buffer = new byte[filestream.Length]; filestream.Read(buffer, 0, buffer.Length); list.AddRange(buffer); } finally { filestream.Close(); } } result = list.ToArray(); } finally { try { if (!String.IsNullOrEmpty(InFile)) { File.Delete(InFile); } } catch {} try { if (!String.IsNullOrEmpty(OutFile)) { File.Delete(OutFile); } } catch {} } } else { result = Encoding.Default.GetBytes(s); } return(result); }
public static string XMLToString(byte[] XML) { string inFile = null, outFile = null; try { string result = string.Empty; if (Encoding.Default.GetString(XML, 0, CompressionUtils.COMPRESSION_SIGNATURE.Length) == CompressionUtils.COMPRESSION_SIGNATURE) { inFile = Path.GetTempFileName(); outFile = Path.GetTempFileName(); using (FileStream fs = new FileStream(inFile, FileMode.Open, FileAccess.Write)) { try { fs.Write( XML, CompressionUtils.COMPRESSION_SIGNATURE.Length, XML.Length - CompressionUtils.COMPRESSION_SIGNATURE.Length ); } finally { fs.Close(); } } CompressionUtils.DecompressFile(inFile, outFile); using (FileStream fs = new FileStream(outFile, FileMode.Open, FileAccess.Read)) { try { StringBuilder sb = new StringBuilder(); const int BUFFER_SIZE = 4096; byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead = 0; while (0 < (bytesRead = fs.Read(buffer, 0, BUFFER_SIZE))) { sb.Append(Encoding.Default.GetString(buffer, 0, bytesRead)); } result = sb.ToString(); } finally { fs.Close(); } } } else { result = Encoding.Default.GetString(XML); } return(result); } finally { if (!String.IsNullOrEmpty(inFile) && File.Exists(inFile)) { try { File.Delete(inFile); } catch {} } if (!String.IsNullOrEmpty(outFile) && File.Exists(outFile)) { try { File.Delete(outFile); } catch {} } } }