Ejemplo n.º 1
0
        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 {}
                }
            }
        }
Ejemplo n.º 2
0
        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 {}
                }
            }
        }