Exemplo n.º 1
0
        public static void Save <T>(string filePath, T objectToSave, bool useBinary)
        {
            using (var stream = PlatformFileAccess.GetSaveFileStream(filePath))
            {
                using (var writer = (useBinary ? XmlDictionaryWriter.CreateBinaryWriter(stream) : XmlWriter.Create(stream)))
                {
                    var serializer = new DataContractSerializer(typeof(T));

                    serializer.WriteObject(writer, objectToSave);

                    writer.Flush();
                }

                stream.Close();
            }
        }
        public static async Task Save <T>(string filePath, T objectToSave, bool useBinary)
        {
            using (var outputStream = await PlatformFileAccess.GetSaveFileStream(filePath))
            {
                using (var stream = outputStream.AsStreamForWrite())
                {
                    using (var writer = (useBinary ? XmlDictionaryWriter.CreateBinaryWriter(stream) : XmlWriter.Create(stream)))
                    {
                        var serializer = new DataContractSerializer(
                            typeof(T),
                            new DataContractSerializerSettings
                        {
                            PreserveObjectReferences = true
                        });

                        serializer.WriteObject(writer, objectToSave);

                        await stream.FlushAsync();
                    }
                }
            }
        }