public void Serialize <T>(string filePath, T data, DataContractSerializeOptions options = null) { try { if (_type == SerializeType.XmlDataContractSerialize) { if (options == null) { throw new ArgumentException("DataContract Serializer needs a DataContractSerialzieOptions!"); } SerializeWithDataContractInternal(data, filePath, options); } else if (_type == SerializeType.XmlSerialize) { SerializeWithXmlSerializerInternal(data, filePath); } if (_type == SerializeType.XmlDataContractSerialize) { var tempFilePathCompressed = GetTempFilePathCompressed(filePath); FileCompressorDecompressor.CompressFileLZMA(filePath, tempFilePathCompressed); File.Delete(filePath); File.Move(tempFilePathCompressed, filePath); } } catch (Exception e) { throw new ArgumentException("Arquivo inválido - Serializador - Salvar", e); } }
private void DeserializeXmlDataContractSerializerInternal <T>(string filePath, out T newData, DataContractSerializeOptions options) { var decompressFilePath = filePath + Guid.NewGuid(); FileCompressorDecompressor.DecompressFileLZMA(filePath, decompressFilePath); var dcs = DataContractSerializeFactory.Create <T>(options); try { using (var gzipStream = new GZipStream(File.OpenRead(decompressFilePath), CompressionMode.Decompress)) using (var reader = XmlReader.Create(gzipStream)) { newData = (T)dcs.ReadObject(reader); } File.Delete(decompressFilePath); } catch (Exception e) { newData = default(T); throw new ArgumentException("Arquivo inválido - Serializador - Abrir", e); } }