In my universal windows app I am storing user data transforming its objects into xml files through XmlSerializer.
The app was compiling, building and running perfectly until somehow (without any change on the code) the build for release started to give me this error: System.InvalidOperationException: Unable to generate a temporary class (result=1).
If I build or run the app on debug it works flawlessly but on release (with has the .NET native tool chain active) I get the error.
I already given permission on C:\Windows\Temp folder to everyone. Even my mother has access to it but the error remains.
If it really is a read/write problem on the XmlSerializer I wonder if there is any way to change the serializer temp folder on UWP project.
Here is the code that I used to serialize objects:
public static async Task<T> ReadObjectFromXmlFileAsync<T>(string filename)
{
// this reads XML content from a file ("filename") and returns an object from the XML
T objectFromXml = default(T);
var serializer = new XmlSerializer(typeof(T));
StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile file = await folder.GetFileAsync(filename).AsTask().ConfigureAwait(false);
Stream stream = await file.OpenStreamForReadAsync().ConfigureAwait(false);
objectFromXml = (T)serializer.Deserialize(stream);
stream.Dispose();
return objectFromXml;
}
public static async Task SaveObjectToXml<T>(T objectToSave, string filename)
{
// stores an object in XML format in file called 'filename'
var serializer = new XmlSerializer(typeof(T));
StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile file = await folder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting).AsTask().ConfigureAwait(false);
Stream stream = await file.OpenStreamForWriteAsync().ConfigureAwait(false);
using (stream)
{
serializer.Serialize(stream, objectToSave);
}
}
//This uses a subfolder
public static async Task<T> ReadObjectFromXmlFileAsync<T>(string filename)
{
// this reads XML content from a file ("filename") and returns an object from the XML
T objectFromXml = default(T);
var serializer = new XmlSerializer(typeof(T));
StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFolder subFolder = await folder.GetFolderAsync("Notes").AsTask().ConfigureAwait(false);
StorageFile file = await subFolder.GetFileAsync(filename).AsTask().ConfigureAwait(false);
Stream stream = await file.OpenStreamForReadAsync().ConfigureAwait(false);
objectFromXml = (T)serializer.Deserialize(stream);
stream.Dispose();
return objectFromXml;
}
What am I missing? Can anyone help?