Click here to Skip to main content
15,887,267 members
Articles / Programming Languages / XML
Tip/Trick

XML Data Binding in Windows 8 apps Using C#

Rate me:
Please Sign up or sign in to vote.
4.43/5 (4 votes)
17 Nov 2014CPOL 12.9K   7   1
What's Data Binding: Data binding is the process that establishes a connection between the application User Interface and business logic.

Introduction

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

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

This is a simple way to 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 renamed it DataBinding.

Description

How does this sample solve the problem?

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

XML
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">     
        <GridView x:Name="DataGrid1">     
            <GridView.ItemTemplate>     
                <DataTemplate>     
                    <Grid Background="Red" Width="300" Height="100">     
                        <TextBlock Text="{Binding Title}"></TextBlock>     
                    </Grid>     
                </DataTemplate>     
            </GridView.ItemTemplate>     
       </GridView>     
   </Grid>    

Then, I created an XML file Fruits.XML in the common file to store the data of solution explorer:

XML
<?xml version="1.0" encoding="utf-8" ?>     
<Fruits>     
   <Fruits name="Apple"/>     
   <Fruits  name="Apricot "/>     
   <Fruits  name="Banana"/>     
   <Fruits  name="Blackberry"/>     
   <Fruits  name="Blackcurrant "/>     
   <Fruits  name="Lemon"/>     
   <Fruits  name="Mango"/>     
          
</Fruits>    

I also created a new class Fruits.cs and added two statements.

C#
using System.Xml.Linq; using Windows.ApplicationModel;  
      
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.).  
using System;     
using System.Collections.Generic;     
using System.Linq;     
using System.Text;     
using System.Threading.Tasks;     
using System.Xml.Linq;     
using Windows.ApplicationModel;     
namespace DataBinding     
{     
    class Fruits     
    {     
        public string Title { get; set; }     
    }     
}   

Finally, the XML file will automatically synchronize with our controls after adding this code:

C#
protected override void OnNavigatedTo(NavigationEventArgs e)     
{     
    //reading xml path      
    string peopleXMLPath = Path.Combine(Package.Current.InstalledLocation.Path, "Common/Fruits.xml");     
    XDocument loadedData = XDocument.Load(peopleXMLPath);     
    //retrieving data from xml using LINQ     
    var data = from query in loadedData.Descendants("Fruits")                           
    select new Fruits     
    {     
        Title = (string)query.Attribute("name")                          
    };     
    //assigning source to GridView Control     
    DataGrid1.ItemsSource = data;     
}  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student TEK-UP
Tunisia Tunisia
Freak, Destructed, Mad, Psycho, overjoyed, techie, Creative, Thinker, Writer, Reader, Blogger, and Finally ENGINEER....

Comments and Discussions

 
GeneralMy vote of 4 Pin
fredatcodeproject17-Nov-14 4:05
professionalfredatcodeproject17-Nov-14 4:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.