在我的通用Windows应用程序中,我存储的用户数据通过XmlSerializer将其对象转换为xml文件。
应用程序正在编译,构建和运行完美,直到某种方式(代码没有任何更改),发布版本开始给我这个错误: System.InvalidOperationException:无法生成临时班级(结果= 1)。
如果我在调试时构建或运行应用程序,它可以完美运行但在发布时(.NET本机工具链处于活动状态)我收到错误。
我已经将C:\ Windows \ Temp文件夹的权限授予所有人。即使是我的母亲也可以访问它,但错误仍然存在。
如果它确实是XmlSerializer上的读/写问题我想知道是否有任何方法可以更改UWP项目上的序列化程序临时文件夹。
以下是我用来序列化对象的代码:
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;
}
我错过了什么?有人可以帮忙吗?
答案 0 :(得分:0)
您是否引用了XML序列化文件中的“添加为链接”的项目? (那是在我的情况下导致上述错误的原因)在这种情况下,这里是......
... SOLUTION:
显然,您仍然可以使用“添加为链接”的代码共享方式,但是为了启用“使用.NET Native工具链编译”而没有任何这些模糊的无关错误,“添加为链接“文件需要移动到调用者项目。
答案 1 :(得分:0)
刚来这里帮助遇到这个问题的任何其他人。似乎当.NET本机正在编译时,如果你的类将被Xml序列化/反序列化,那么你将遇到问题,这些类包含对HorizontalAlignment和VerticalAlignment之类的东西的引用。真烦人,我目前正在使用Visual Studio Update 3,我仍然有这个问题。
有关更好的解释,请参阅此堆栈溢出答案:
http://stackoverflow.duapp.com/questions/37680536/cant-build-uwp-in-release-mode/37865469#37865469
答案 2 :(得分:0)
我对这个几乎相同的代码有完全相同的错误:
Public Async Function SerializeObjectToStorage(ByVal obj As Object, ByVal filename As String) As Task(Of Boolean)
Dim localFolder As Windows.Storage.StorageFolder
Try
Dim objStringWriter As New StringWriter
Dim x As New XmlSerializer(obj.GetType)
x.Serialize(objStringWriter, obj)
localFolder = Windows.Storage.ApplicationData.Current.LocalFolder
Dim myFile As Windows.Storage.StorageFile = Await localFolder.CreateFileAsync(filename, Windows.Storage.CreationCollisionOption.ReplaceExisting)
Dim stream = Await myFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite)
Using outputStream = stream.GetOutputStreamAt(0)
Dim dataWriter As New DataWriter(outputStream)
dataWriter.WriteString(objStringWriter.ToString)
Await dataWriter.StoreAsync()
Await outputStream.FlushAsync()
stream.Dispose()
End Using
Catch ex As Exception
Return False
End Try
Return True
End Function
Public Async Function DeserializeStorageToObject(ByVal obj As Object, ByVal filename As String) As Task(Of Object)
Dim localFolder As Windows.Storage.StorageFolder
Try
Dim objStringWriter As New StringWriter
Dim x As New XmlSerializer(obj.GetType)
x.Serialize(objStringWriter, obj)
localFolder = Windows.Storage.ApplicationData.Current.LocalFolder
Dim myFile As Windows.Storage.StorageFile = Await localFolder.GetFileAsync(filename)
Dim stream = Await myFile.OpenAsync(Windows.Storage.FileAccessMode.Read)
Dim size = stream.Size
Using inputStream = stream.GetInputStreamAt(0)
Dim dataReader As New DataReader(inputStream)
Dim numBytesLoaded As UInteger = Await dataReader.LoadAsync(CUInt(size))
Dim text As String = dataReader.ReadString(numBytesLoaded)
Using tr = New StringReader(text)
obj = x.Deserialize(tr)
Return obj
End Using
stream.Dispose()
End Using
Catch ex As Exception
Return Nothing
End Try
Return True
End Function
我的结论是,.NET Native编译器实际上会尝试序列化您尝试序列化您尝试使用该代码序列化的任何对象的任何类型。就我而言,类型很简单:
Public Class Result
Public Property ElementName As String
Public Property Id As Integer
Public Property Name As String
End Class
Public Class GetValues
Public Property Count As Integer
Public Property Message As String
Public Property SearchCriteria As String
Public Property Results As Result()
End Class
但是,这些完全相同的函数无法使用WCF服务的数据协定序列化对象。所以我想也许它需要是你的类型的ObservableCollection。我还没有尝试过。我会用答案更新这个。