返回具有特定扩展名的字符串的方法

时间:2016-04-05 14:07:35

标签: c# xmlserializer gzipstream

我很长时间试图解决一个问题。我有一个序列化字符串的方法,遵循代码:

XmlRetorno()

        var algumasDef = new XmlWriterSettings {
            Indent = true, 
            OmitXmlDeclaration = true
        };
        var nameSpace = new XmlSerializerNamespaces();
        nameSpace.Add(string.Empty, "urn:sngpc-schema");
        var meuXml = new XmlSerializer(GetType(), "urn:sngpc-schema"); 

        using (var minhaString = new StringWriterWithEncoding(Encoding.GetEncoding("iso-8859-1"))) {
            using (var escreve = XmlWriter.Create(minhaString, algumasDef)) {
                meuXml.Serialize(escreve, this, nameSpace);
            }
            return minhaString.ToString();
        }

然后,我的下一步是将该字符串压缩为zip文件,我的方法是压缩。

CompactXml()

        string ziparEssaString = msg.XmlRetorno();

        byte[] byteArray = new byte[ziparEssaString.Length];
        int indexBA = 0;
        foreach (char item in ziparEssaString.ToArray()) {
            byteArray[indexBA++] = (byte)item;
        }
        //prepare to compress
        using (MemoryStream ms = new MemoryStream()) {
            using (GZipStream sw = new GZipStream(ms, CompressionMode.Compress)) {
                sw.Write(byteArray, 0, byteArray.Length);
            }

            //transform bytes[] zip to string
            byteArray = ms.ToArray();
            StringBuilder sB = new StringBuilder(byteArray.Length);
            foreach (byte item in byteArray) {
                sB.Append((char)item);
            }
            return sB.ToString();
        }

我需要压缩格式化.xml的字符串,当我解压缩时我需要扩展名为.xml,我的webservice返回错误。拜托,我需要一盏灯。

0 个答案:

没有答案