Feedback

C# - XML Objekt async mit FilePicker schreiben und lesen

Veröffentlicht von am 10/28/2015
(0 Bewertungen)
XML Objekte async mit FileSavePicker schreiben und mit FileOpenPicker lesen.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Windows.Storage;
using Windows.Storage.Pickers;
using System.Xml.Serialization;
using System.IO;

namespace XML
{
    public class WriteReadXML
    {

        /// <summary>
        /// Write XML File async with FilePicker
        /// </summary>
        /// <typeparam name="T">XML Class</typeparam>
        /// <param name="objectToWrite"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        public async Task Write_With_FileSavePicker<T>(T objectToWrite, string filename)
        {
            try
            {
                FileSavePicker writePicker = new FileSavePicker();

                // Set the file type to the store
                writePicker.FileTypeChoices.Add("XML Document", new List<string>() { ".xml" });

                // Default file name
                writePicker.SuggestedFileName = filename;

                // Initial search path
                writePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;

                // File selection
                StorageFile file = await writePicker.PickSaveFileAsync();
                if (file != null)
                {
                    // Write XML object to file
                    XmlSerializer serializer = new XmlSerializer(typeof(T));
                    var fileToken = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(file);
                    Stream stream = await file.OpenStreamForWriteAsync();
                    using (stream)
                    {
                        serializer.Serialize(stream, objectToWrite);
                    }
                    // Message "OK"		
                }
                else
                {
                    // Message "No selection"
                }
            }
            catch
            {
                // Message "Error reading"
            }
        }
        /// <summary>
        /// Read XML File async with FilePicker
        /// xml_Object = await $LoadXMLFile$<XML_Class>();
        /// </summary>
        /// <typeparam name="T">XML Class</typeparam>
        /// <returns>XML object or default(XML Class)</returns>
        public async Task<T> ReadXML_With_FileOpenPicker<T>()
        {
            try
            {
                FileOpenPicker readPicker = new FileOpenPicker();

                // Explorer type of advertisement
                readPicker.ViewMode = PickerViewMode.List;

                // nitial search path
                readPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;

                // Default of file types
                readPicker.FileTypeFilter.Add(".xml");

                // File selection
                StorageFile file = await readPicker.PickSingleFileAsync();
                if (file != null)
                {
                    // reads from a file and returns an XML object
                    T objectFromXml = default(T);
                    var serializer = new XmlSerializer(typeof(T));
                    Stream stream = await file.OpenStreamForReadAsync();
                    objectFromXml = (T)serializer.Deserialize(stream);
                    stream.Dispose();
                    return objectFromXml;
                }
                else
                {
                    // Message "No selection"
                }
            }
            catch
            {
                // Message "Error reading"
            }
            return default(T);
        }

    }
}

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!