18

在 windows-8 中创建对象时遇到问题StreamWriter,通常我只是创建一个实例,只是将字符串作为参数传递,但在 Windows 8 中,我收到一个错误,表明它应该接收一个 Stream,但我注意到 Stream是一个抽象类,有人知道如何编写 xml 文件的代码吗?顺便说一句,我使用 .xml 是因为我想保存序列化对象,或者有人知道如何将序列化对象保存到文件中视窗 8?

有任何想法吗?

目前使用的是 Windows 8 Consumer Preview

代码:

StreamWriter sw = new StreamWriter("person.xml");

错误:

The best overloaded method match for 'System.IO.StreamWriter.StreamWriter(System.IO.Stream)' has some invalid arguments

4

2 回答 2

27

而不是 StreamWriter 你会使用这样的东西:

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();
    }
}

您可以查看 WinRTXamlToolkit 库中的StringIOExtensions以供示例使用。

编辑*

虽然以上所有内容都应该有效 - 它们是在FileIO类在 WinRT 中可用之前编写的,这简化了上述解决方案解决的大多数常见场景,因为您现在可以调用await FileIO.WriteTextAsync(file, contents)将文本写入文件,并且还有类似的读取方法,写入或附加字符串、字节、字符串列表或IBuffers.

于 2012-04-24T07:46:39.483 回答
4

您可以创建一个通用的静态方法,您可以像这样在整个应用程序中使用它

 private async Task<T> ReadXml<T>(StorageFile xmldata)
    {
        XmlSerializer xmlser = new XmlSerializer(typeof(List<myclass>));
        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>(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());

        }

    }

编写 xml 只需使用

 StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("data.xml",CreationCollisionOption.ReplaceExisting);
        await  writeXml(Data,file);

并阅读 xml 使用

  StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("data.xml");
      Data =  await  ReadXml<List<myclass>>(file);
于 2013-04-09T14:48:52.257 回答