News
  • Favorite
  • Facebook
  • Twitter
  • LinkedIn

Microsoft Press Pearson Education (InformIT)
Skip Navigation Links
Pearson Education (InformIT)
How to: Read XML files in Windows 8 Store Apps Package

Sometimes you have you add basic data in XML files and add these files to your Windows 8 Store application as a resource. You want to read the contents of the XML file to display these data in application pages. In this article we will create a Windows Store App that will read XML data from an XML file that is added to the application package.

Create a Blank Windows Store App

1.      Open Visual Studio 2012 Express for Windows 8.

2.      Click on New Project. The New Project Dialog box appears.

3.      From the left pane select C# and then Windows Store Templates.

4.      From the right pane select Blank App (XAML).

5.      Type your project name and then click Ok. We set the project name to ReadXmlApp.

New Project Dialog

Add Xml file and fill it with data

1.      Right click on the ReadXmlApp in the Solution Explorer and select Add then New Item. The Add New Item Dialog appears.

2.      From the left pane select C# and then Data.

3.      From the right pane select XML file and type BasicData.xml in the Name field.

4.      Then Click Add button to add the xml file to the project.


New Project Dialog

5.      Add the following xml to the BasicData.xml.

<?xml version="1.0" encoding="utf-8" ?>

<Orders>

  <Order>

    <OrderId>1001</OrderId>

    <OrderTotal>5000</OrderTotal>

    <Customer>Muhammad</Customer>

    <Phone>0123456789</Phone>

  </Order>

  <Order>

    <OrderId>1011</OrderId>

    <OrderTotal>5000</OrderTotal>

    <Customer>Jone</Customer>

    <Phone>0123466789</Phone>

  </Order>

  <Order>

    <OrderId>1023</OrderId>

    <OrderTotal>5000</OrderTotal>

    <Customer>Fouad</Customer>

    <Phone>01244456789</Phone>

  </Order>

  <Order>

    <OrderId>1034</OrderId>

    <OrderTotal>5000</OrderTotal>

    <Customer>Ahmad</Customer>

    <Phone>0123456781</Phone>

  </Order>

</Orders>

Create the UI and adding Xaml Code

1.      Open the MainPage.xaml file by double clicking the MainPage.xaml file from the Solution Explorer.

2.      Select the XAML view and find the Grid Control in the MainPage.xaml.

3.      Add the following code to the Grid in the MainPage.xaml.

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">

    <Grid.RowDefinitions>

        <RowDefinition Height="1*"/>

        <RowDefinition Height="8*"/>

    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>

        <ColumnDefinition Width="1*"/>

        <ColumnDefinition Width="3*"/>

    </Grid.ColumnDefinitions>

    <Button Name="LoadXmlButton"Content="Load Xml" FontSize="20" Grid.Row="0" Grid.Column="1" Click="LoadXmlButton_Click"/>

    <TextBlock Name="XmlTextBlock"Grid.Row="1" Grid.Column="1"/>

</Grid>

a.      The above XAML code creates a Grid control with two columns and two rows.

b.     Adds a Button and name it LoadXmlButton, set the LoadXmlButton font size to 20.

c.      It places the LoadXmlButton in first row using Grid.Row attached property and second column using Grid.Column attached property.

d.     Add a TextBlock control and name it XmlTextBlock.

e.     It places the XmlTextBlock in second row using Grid.Row attached property and second column using Grid.Column attached property.

4.      Switch to the Design View and you will see something like the following image:


New Project Dialog

Adding C# Code and reading XML file content

1.      Double click the LoadXmlButton, this will create the click event handler for the LoadXmlButton.

2.      Add the following using statements to the start of the code behind file

using Windows.Storage;

using Windows.ApplicationModel;

3.      Windows.Storage namespace manages files, folders, and application settings.

4.      Windows.ApplicationModel namespace provides an app with access to core system functionality and run-time information about its app package.

5.      Mark the LoadXmlButton_Click method as asynchronous using the async keyword.

6.      Add the following code to the LoadXmlButton_Click method

 

private async void LoadXmlButton_Click(object sender, RoutedEventArgs e)

{

    try

    {

        StorageFolder storageFolder = Package.Current.InstalledLocation;

        StorageFile storageFile = await storageFolder.GetFileAsync("BasicData.xml");

 

        XmlTextBlock.Text = await FileIO.ReadTextAsync(storageFile, Windows.Storage.Streams.UnicodeEncoding.Utf8);

 

    }

    catch (Exception ex)

    {

        XmlTextBlock.Text = ex.Message;

    }

}

a.      The above code creates an object from the StorageFolder class, which manipulates folders and their contents, and provides information about them.

b.     Sets the storageFile object to the current installation folder of our application.

c.      Creates an object from the StorageFile class, which represents a file. Provides information about the file and its content, and ways to manipulate them.

d.     We then get our XML file from the storage folder by calling the asynchronous method GetFileAsync and passing it the file name.

e.     We then use the FileIO.ReadTextAsync asynchronous method to read the contents of the file with the UTF-8 encoding and assign the returned text to the Text property of the XmlTextBlock.

f.       If there is any exception it will display it in the XmlTextBlock.

7.      Build your application and run it in the Simulator. You will find something similar to the following image

New Project Dialog

8.      Click on the Load Xml Button. This will load the XML from the BasicData.xml file and display it on the TextBlock.

New Project Dialog

Now you have a Windows Store Application that read XML data from an XML file that is added to the application package and display it in a TextBlock.

(144) Comments

Blog Message http://www.eelabel.com

What as Happening i am new to this, I stumbled upon this I ave found It absolutely helpful and it has aided me out loads. I hope to contribute & aid other users like its helped me. Good job.


Blog Message http://www.russiansoverseas.com/members/toptown3/activity/12886929/

Thanks so much for the blog.Really looking forward to read more. Fantastic.


Blog Message http://viewprivateinstagrams.com/

It as not that I want to copy your web-site, but I really like the style. Could you let me know which style are you using? Or was it custom made?


Blog Message http://www.sattamaster.in

Very Fascinating Blog! Thank You For This Weblog!


Blog Message http://manchesterburntly.webgarden.com/section-1/manchesterburntly-s-blog/the-best-time-to-call-a-personal

Very informative blog article. Really Cool.


Blog Message http://www.stumbleupon.com/stumbler/hyundaigialai3s

I value the blog.Really looking forward to read more. Cool.


Blog Message https://saboresdechurrasco.wordpress.com/

I truly appreciate this post. I ave been looking everywhere for this! Thank goodness I found it on Bing. You have made my day! Thx again!


Blog Message http://www.swissdent.com.pe/

Well I definitely liked reading it. This post procured by you is very useful for accurate planning.


Blog Message https://foursquare.com/user/382071675/list/httpwwwtadalafilhcom

Im obliged for the post.Thanks Again. Much obliged.


Blog Message https://www.evernote.com/shard/s358/sh/20c84bc1-1450-4117-b76b-a8c77e8d314e/e832a6d3661e8c146313f1e745895fdf

A big thank you for your article post.Really thank you! Will read on...


Blog Message http://etgold.blogspot.tw/2016/09/alien-implants-removed-with-physical.html

This is very interesting, You are a very skilled blogger. I ave joined your rss feed and look forward to seeking more of your excellent post. Also, I ave shared your website in my social networks!


Blog Message http://footcareguru.com/most-comfortable-steel-toe-shoes/

Very interesting information!Perfect just what I was searching for! It as the Brady Act taking manpower and crime-fighting capability off the streets. by Dennis Martin.


Blog Message http://muscledollar6.blog.fc2.com/blog-entry-1.html

Thank you ever so for you blog post.Really looking forward to read more. Really Great.


Blog Message http://promoments.com/members/sprucecoach40/activity/340815/

I am so grateful for your post.Thanks Again. Cool.


Blog Message http://www.bagtheweb.com/b/kAL0fb

I appreciate you sharing this post. Fantastic.

First Previous 1 2 3 4 5 6 7 8 9 10 Next Last 
Rating
Name
Email
Comment