Forum Cross Platform with Xamarin

Generating XML file in PCL


I have created an app using PCL in which the user types in some information in simple editors/textboxes and so on which are on different pages (for example Page 1: user types in his name, Page2: user types in his last name). Now I want to create an XML file with all those information.

I created a class with the XML elements I like and used System.Xml.Serialization to name everything. What I don't seem to understand is how do I create the XML file? I tried this:

        //FileStream not supported in PCL?!
        using (FileStream fileStream = new FileStream("fileName.xml", FileMode.OpenOrCreate))
            using (StreamWriter sw = new StreamWriter(fileStream, Encoding.UTF8))
                MySerializableClass msc = new MySerializableClass();
                XmlSerializer serializer = new XmlSerializer(typeof(MySerializableClass));
                serializer.Serialize(sw, msc);

Where do I put this code? Should I put it in App.cs?



  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    edited February 2017

    //FileStream not supported in PCL?!

    I do a DependencyService for each platform that is my FileHelper.
    make an IFileHelper interface, make a dependencyservice for it... blah blah... from Xamarin site examples on making a DS

    Then you can add methods for CreateFileStream, GetFolderCreationDate etc. etc. All those methods that need to be platform specific calls.
    I also include things like DeleteFolder even though you can call that from PCL, just to have a centralized implementation for emptying the folder first (You can't delete a folder if it has contents).

Sign In or Register to comment.