1

我尝试从我的 Windows 应用程序中读取 xml 文件。在这个中,我在 Assets/Mocks/clubic.xml(构建操作:内容)中添加了一个文件夹,以将 xml 文件中的数据用作模拟。所以我尝试使用以下代码

         var package = Windows.ApplicationModel.Package.Current;
            var installedLocation = package.InstalledLocation;
            try
            {
                StorageFile sampleFile = await installedLocation.GetFileAsync("Assets/Mocks/clubic.xml");
            }
            catch (Exception ex)
            {

                throw ex;
            }

            try
            {
                StorageFile sampleFile = await installedLocation.GetFileAsync("ms-appx:///Assets/Mocks/clubic.xml");
            }
            catch (Exception ex)
            {

                throw ex;
            }

对于这两种情况,我获得了相同的异常 System.ArgumentException: Value does not fall in the expected range。在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 在 FileReadWrite.MainPage.d__17.MoveNext ()}

我尝试使用此代码

StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
            if (local != null)
            {
                // Get the DataFolder folder.
                var dataFolder = await local.GetFolderAsync("Assets/Mocks");

                // Get the file.
                var file = await dataFolder.OpenStreamForReadAsync("clubic.xml");

                // Read the data.
                using (StreamReader streamReader = new StreamReader(file))
                {
                    this.textBlock1.Text = streamReader.ReadToEnd();
                }

            }

我仍然遇到同样的问题。你能帮助我吗。

最好的问候,亚历山大

4

2 回答 2

4

尝试这个

StorageFile sampleFile = await installedLocation.GetFileAsync( @"Assets\Mocks\clubic.xml" );

于 2013-10-30T21:54:45.040 回答
3

这是我在我的应用程序中为读取构建为内容的文件所做的操作:

var resource = System.Windows.Application.GetResourceStream(new Uri(@"Assets\Mocks\clubic.xml", UriKind.Relative));
using (StreamReader sr = new StreamReader(resource.Stream)) {
    this.textBlock1.Text = await sr.ReadToEndAsync();
}
于 2013-10-31T04:38:16.503 回答