Ho problemi durante la creazione di un object StreamWriter
in windows-8, in genere creo solo un’istanza passando semplicemente una stringa come parametro, ma in Windows 8 viene visualizzato un errore che indica che deve ricevere un stream, ma ho notato che Stream è una class astratta, Qualcuno sa come sarà il codice per scrivere un file xml ?, BTW Sto usando .xml perché voglio salvare l’object serializzato, o qualcuno sa come salvare su un file un object serializzato in Windows 8 ?.
Qualche idea?
Attualmente utilizza Windows 8 Consumer Preview
Codice:
StreamWriter sw = new StreamWriter("person.xml");
Errore:
The best overloaded method match for 'System.IO.StreamWriter.StreamWriter(System.IO.Stream)' has some invalid arguments
Invece di StreamWriter dovresti usare qualcosa del genere:
StorageFolder folder = ApplicationData.Current.LocalFolder; StorageFile file = await folder.CreateFileAsync(); using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.ReadWrite)) { using (IOutputStream outputStream = fileStream.GetOutputStreamAt(0)) { using (DataWriter dataWriter = new DataWriter(outputStream)) { //TODO: Replace "Bytes" with the type you want to write. dataWriter.WriteBytes(bytes); await dataWriter.StoreAsync(); dataWriter.DetachStream(); } await outputStream.FlushAsync(); } }
È ansible esaminare la class StringIOExtensions nella libreria WinRTXamlToolkit per l’utilizzo di esempio.
MODIFICARE*
Mentre tutto questo dovrebbe funzionare – sono stati scritti prima che la class FileIO
diventasse disponibile in WinRT, il che semplifica la maggior parte degli scenari comuni risolti dalla soluzione di cui sopra in quanto ora è sufficiente richiamare await FileIO.WriteTextAsync(file, contents)
per scrivere testo in file e ci sono anche metodi simili per leggere, scrivere o aggiungere stringhe, byte, liste di stringhe o IBuffers
.
È ansible creare un metodo statico comune che è ansible utilizzare attraverso un’applicazione come questa
private async Task ReadXml (StorageFile xmldata) { XmlSerializer xmlser = new XmlSerializer(typeof(List)); T data; using (var strm = await xmldata.OpenStreamForReadAsync()) { TextReader Reader = new StreamReader(strm); data = (T)xmlser.Deserialize(Reader); } return data; } private async Task writeXml(T Data, StorageFile file) { try { StringWriter sw = new StringWriter(); XmlSerializer xmlser = new XmlSerializer(typeof(T)); xmlser.Serialize(sw, Data); using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.ReadWrite)) { using (IOutputStream outputStream = fileStream.GetOutputStreamAt(0)) { using (DataWriter dataWriter = new DataWriter(outputStream)) { dataWriter.WriteString(sw.ToString()); await dataWriter.StoreAsync(); dataWriter.DetachStream(); } await outputStream.FlushAsync(); } } } catch (Exception e) { throw new NotImplementedException(e.Message.ToString()); } }
scrivere xml semplicemente usare
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("data.xml",CreationCollisionOption.ReplaceExisting); await writeXml(Data,file);
e per leggere l’uso di xml
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("data.xml"); Data = await ReadXml>(file);