XML Data Binding in Windows 8 apps Using C#

In this blog you will learn how to XML data in Windows 8 apps Using C#.

Introduction

What's Data Binding: Data binding is the process that establishes a connection between the application User Interface and business logic.

This sample explain how to Bind Data to a Grid View using XML file.

This is a simple way the store Data because you just need an XML file to store and transport Data.

So this sample is very helpful for your windows store applications:

Building the Sample

I used Visual Studio 2012 Ultimate edition.

I created a new project (Blank application C#) and rename it DataBinding.

Description

How does this sample solve the problem?

First, I added a Grid View Control with item and data templates to bind data to the GridView control as follows :

  1.  <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">     
  2.          <GridView x:Name="DataGrid1">     
  3.              <GridView.ItemTemplate>     
  4.                  <DataTemplate>     
  5.                      <Grid Background="Red" Width="300" Height="100">     
  6.                          <TextBlock Text="{Binding Title}"></TextBlock>     
  7.                      </Grid>     
  8.                  </DataTemplate>     
  9.              </GridView.ItemTemplate>     
  10.         </GridView>     
  11.     </Grid>    
Than, created a XML file Fruits.XML in the common file to store the data of solution explorer:
  1.  <?xml version="1.0" encoding="utf-8" ?>     
  2.  <Fruits>     
  3.     <Fruits name="Apple"/>     
  4.     <Fruits  name="Apricot "/>     
  5.     <Fruits  name="Banana"/>     
  6.     <Fruits  name="Blackberry"/>     
  7.     <Fruits  name="Blackcurrant "/>     
  8.     <Fruits  name="Lemon"/>     
  9.     <Fruits  name="Mango"/>     
  10.       
  11.  </Fruits>    
Also, created a new Class Fruits.cs

And added two statments.
  1. using System.Xml.Linq; using Windows.ApplicationModel;  
  2.   
  3. using System.Xml.Linq; //(Contains the classes for LINQ to XML. LINQ to XML is an in-memory XML programming interface that enables you to modify XML documents efficiently and easily.).  
  1.   using System;     
  2.   using System.Collections.Generic;     
  3.   using System.Linq;     
  4.   using System.Text;     
  5.   using System.Threading.Tasks;     
  6.   using System.Xml.Linq;     
  7.   using Windows.ApplicationModel;     
  8.   namespace DataBinding     
  9.   {     
  10.        class Fruits     
  11.       {     
  12.           public string Title { getset; }     
  13.       }     
  14.   }   
Finally the XML file will automaticlly synchronize with our controls after adding this code:
  1.  protected override void OnNavigatedTo(NavigationEventArgs e)     
  2.  {     
  3.       //reading xml path      
  4.       string peopleXMLPath = Path.Combine(Package.Current.InstalledLocation.Path, "Common/Fruits.xml");     
  5.       XDocument loadedData = XDocument.Load(peopleXMLPath);     
  6.       //retriving data from xml using LINQ     
  7.       var data = from query in loadedData.Descendants("Fruits")                           
  8.       select new Fruits     
  9.       {     
  10.             Title = (string)query.Attribute("name")                          
  11.       };     
  12.       //assigning source to GridView Control     
  13.       DataGrid1.ItemsSource = data;     
  14.  }