Normally when you start developing application in XAML and windows 8 you will include some files in your solution that you will use to display pictures, data and read xml files.
I’m a Developers and like all developers I love to jump in code then start thinking how can I do something when I face a problem, but you need to know a few things before this understanding the namespace Windows.Storage, Default paths and files
Windows.Storage Namespace
If you take a look at the Windows.Storage documentation, you'll discover that the Windows.Storage namespace has a StorageFolder class that lets you manipulate folders and their content. By exploring the class further, you'll find that it contains a method called StorageFolder.CreateFileAsync. This method will asynchronously create a new file in the current folder.
You will find also a method for getting files, read files attributes .. etc
In my Below Example I’ve added an XML document that contains list of Countries to retrieve and display in my Application.
first you need to know where is your assets folder is installed on the user machine.
- StorageFolder InstallationFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
then you will need to read the files from the location you added it to the assets folder
- string CountriesFile = @"Assets\Countries.xml";
- StorageFolder InstallationFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
- StorageFile file = await InstallationFolder.GetFileAsync(CountriesFile);
In my case I converted the file into steam and started using Linq for XML and started reading the data from the XML
Below If my Full Code listing for the Load countries Method it might be useful.
- public async void LoadCountriesXML()
- {
- string CountriesFile = @"Assets\Countries.xml";
- StorageFolder InstallationFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
- StorageFile file = await InstallationFolder.GetFileAsync(CountriesFile);
- Stream Countries = await file.OpenStreamForReadAsync();
- XDocument xDOC = XDocument.Load(Countries);
- IEnumerable<XElement> listofCountries = from country in xDOC.Descendants("Country")
- select country;
- foreach(XElement item in listofCountries)
- {
- TripCountryView tripCountryView = new TripCountryView();
- tripCountryView.SetControlData(item.Attribute("Name").Value, item.Attribute("ID").Value, item.Attribute("FlagImage").Value);
- pnlCountries.Items.Add(tripCountryView);
- }
- }