Windows. Storage namespace
First, we need to know the Windows. Store namespace.
If you read the Windows. Storage documentation provided by Microsoft MSDN, you will find a StorageFolder class in it. Through this class, we can operate folders and related content. The StorageFolder class has a method called StorageFolder. CreateFileAsync, which asynchronously creates a file in the current folder. At the same time, you will also see that there are methods to get the file content, read file attributes, and so on.
The operation code is as follows:
async void MainPage_Loaded(object sender, RoutedEventArgs e) { StorageFolder InstalltionFolder = Windows.ApplicationModel.Package.Current.InstalledLocation; string CountriesFile = @"Assets\output.xml"; StorageFile file = await InstalltionFolder.GetFileAsync(CountriesFile); Stream countries = await file.OpenStreamForReadAsync(); XDocument doc = XDocument.Load(countries); }
Local XML file:
<?xml version="1.0" encoding="utf-8" ?><bookstore><book category="CHLDREN"> <title>Harry Potter</title> <author>JK. Rowling</author> <year>2005</year> <price>39.95</price></book><book category="WEB"> <title>Harry Potter</title> <author>JK. Rowling</author> <year>2005</year> <price>39.95</price></book></bookstore>
Here, we need to pay special attention to the async and await keywords, which are the new keywords in C #. Here we only need to know that we need to use them when calling asynchronous functions, I will explain async and await in detail in the subsequent study series.
Running effect: