FileIO.ReadTextAsync 返回“空字符串".写入xml文件时出错 [英] FileIO.ReadTextAsync returns "Empty String". Error while writing xml fil

查看:21
本文介绍了FileIO.ReadTextAsync 返回“空字符串".写入xml文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FileIO.ReadTextAsync 在 Windows 8 应用程序中总是返回空字符串 ("").正在开发通用应用程序,需要注意的是,FileIO.ReadTextAsync 在 Windows Phone 应用程序中部署时返回字符串,但在 Windows 8 应用程序中返回空字符串?这是我的代码.

FileIO.ReadTextAsync always returns empty string ("") in Windows 8 application. Am working on Universal application and to note is that, FileIO.ReadTextAsync returns string when deploying in Windows Phone application but returns empty string in Windows 8 application? Here is my code.

async Task<string> LoadStatus(String listFileName)
{
    StorageFile localFile = await ApplicationData.Current.LocalFolder.GetFileAsync(listFileName);
    string configData = await FileIO.ReadTextAsync(localFile);
    return configData;
}

我调用这个函数为,

string result = await LoadStatus(AppConstants.VeryHappyStatusListName);

并且 listFileName 是FileName.xml".

And listFileName is "FileName.xml".

我已经完成了这些(post1post2) 发布并遵循相同的过程但仍然返回空值?

I have gone through these (post1 and post2) posts and followed the same procedure but still returns null?

还有一件事要注意,写入数据时出错.有时它完美地写入数据,有时我在写入时出错.

这是我的保存到存储方法.

async public void SaveStatus(List<Datum> list, String listFileName)
        {
            try
            {
                string localData = ObjectSerializer<List<Datum>>.ToXml(list);

                if (!string.IsNullOrEmpty(localData))
                {
                    StorageFile localFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(listFileName, CreationCollisionOption.ReplaceExisting);

                    if (localFile != null)
                    {
                        //await Task.Delay(3000);
                        await FileIO.WriteTextAsync(localFile, localData);
                    }
                }
            }
            catch (Exception ex)
            {
                DebugPrint(ex.Message.ToString());
                DebugPrintLocation("SaveStaus", "SessionStorage");
            }
        }

我正在关注此链接:https://code.msdn.microsoft.com/windowsapps/CSWinStoreAppSaveCollection-bed5d6e6

还要注意的是,如果我使用断点检查每一行,一切都会很好.我可以保存大列表并检索.但是没有断点就无法控制.

Also important thing to note that, if I check each line using breakpoints everything goes fine. I can able to save large list and retrieve. But couldn't control without breakpoints.

推荐答案

这应该适用于写作.最好解释一下您在发布问题时遇到的具体错误.

This should work for writing. It is also probably a good idea to explain which error you get exactly when you post a question.

public async Task WriteDataToFileAsync(string fileName, string content)
{
    byte[] data = System.Text.Encoding.Unicode.GetBytes(content);
    var folder = KnownFolders.PicturesLibrary;
    var file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
    using (var s = await file.OpenStreamForWriteAsync())
    {
        await s.WriteAsync(data, 0, data.Length);
    }
}

这篇关于FileIO.ReadTextAsync 返回“空字符串".写入xml文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆