---
title: "How to use XML in Windows Phone 7 Development"  
description: "This article is going to explain about how to use XML file in Windows Phone 7 Development. Let’s see a brief demonstration on it."  
author: "Anonymous User"  
published: 2012-04-16  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/925/how-to-use-xml-in-windows-phone-7-development  
category: "windows phone"  
tags: ["windows phone"]  
reading_time: 2 minutes  

---

# How to use XML in Windows Phone 7 Development

This article is going to explain about how to use [XML file](https://www.mindstick.com/articles/75/how-to-read-and-write-xml-file-through-c-sharp) in Windows Phone 7 Development. Let’s see a brief demonstration on it.\

##### Getting started:

1. Open [Visual Studio](https://www.mindstick.com/articles/12378/visual-studio-for-mac-is-out-of-beta-preview-now-officially-available)

2. Create new [Silverlight](https://www.mindstick.com/blog/54/silverlight-in-asp-dot-net) Windows 7 Phone [Development Project](https://www.mindstick.com/blog/11637/how-to-choose-the-right-web-designer-for-your-web-design-development-project)

3. Enter [your project](https://www.mindstick.com/forum/156583/best-way-to-link-bootstrap-file-in-your-project) Name

4. Click on button ‘Ok’

![How to use XML in Windows Phone 7 Development](https://www.mindstick.com/mindstickarticle/23cd7317-5071-4386-b6cf-def945f802ef/images/e5888a42-8c63-40e9-bd3c-b64c5f61a1d6.png)

Now drag and drop ListBox control in **MainPage.xaml** interface design. Write down following code for ListBox control to add content in it from xml file.

![How to use XML in Windows Phone 7 Development](https://www.mindstick.com/mindstickarticle/23cd7317-5071-4386-b6cf-def945f802ef/images/5c18afdf-974b-4dc7-92fe-e5ef21253728.png)

```
<ListBox Height="573" HorizontalAlignment="Left" Margin="12,28,0,0" Name="lstboxAuthors" VerticalAlignment="Top" Width="472" ItemsSource="{Binding}" Loaded="lstboxAuthors_Loaded">                <ListBox.ItemTemplate>                    <DataTemplate>                        <StackPanel Orientation="Horizontal">                            <Image  Source="{Binding AuthorImage}" Width="150" Stretch="Fill" HorizontalAlignment="Center" />                            <StackPanel Width="370">                                <TextBlock Text="{Binding AuthorName}" FontWeight="Bold" FontSize="22"  />                                <TextBlock Text="{Binding Description}" />                                <TextBlock Text="{Binding Source}" />                            </StackPanel>                        </StackPanel>                    </DataTemplate>                </ListBox.ItemTemplate>             </ListBox>
```

\

Now create your XML file which contents will be visible in **ListBox** control.\

##### wpdAuthorDemo.xml:

```
<?xml version="1.0"encoding="utf-8" ?><Authors>  <Author AuthorId="1"                    AuthorName="Arun Kumar Singh"           Description="Arun Kumar Singh is a Jr. Software Developer in MindStick Software Pvt. Ltd."           AuthorImage="http://mindstick.com/UserPics/137.png"          Source="http://mindstick.com/ArticleList.aspx?id=137" />  <Author AuthorId="2"           AuthorName="Awadhendra Kumar Tiwari"           Description="Awadhendra Kumar Tiwari is a Sr. Software Developer in MindStick Software Pvt. Ltd. He is working in MindStick Software Pvt. Ltd. since 3 Year."           AuthorImage="http://mindstick.com/UserPics/45.png"          Source="http://mindstick.com/ArticleList.aspx?id=45" />           <Author AuthorId="3"           AuthorName="Amit Singh"           Description="Amit Singh is a Sr. Software Developer in MindStick Software Pvt. Ltd. He is working in MindStick Software Pvt Ltd. since 5 Years."           AuthorImage="http://mindstick.com/UserPics/6.png"          Source="http://mindstick.com/ArticleList.aspx?id=6" />  <Author AuthorId="4"           AuthorName="Rohit Kesharwani"           Description="Arun Kumar Singh is a Jr. Software Developer in MindStick Software Pvt. Ltd."           AuthorImage="http://mindstick.com/UserPics/156.png"          Source="http://mindstick.com/BlogList.aspx?id=156" /></Authors>
```

\

AuthorImage="http://mindstick.com/UserPics/137.png" Source="http://mindstick.com/ArticleList.aspx?id=137" />

**Now to bind this [file data](https://www.mindstick.com/forum/30/read-xml-file-data-in-c-sharp) with [ListBox control](https://www.mindstick.com/articles/303/listbox-control-in-vb-dot-net), first of all make a class with some property through which we can set AuthorName, Description, AuthorImage etc.**

##### ‘Authors.cs’ Class:

```
using System;using System.Windows.Media;namespace xmlDemo{    public class Authors    {        //get and set AuthorId from wpdAuthorDemo.xml file        public int AuthorId { get; set; }        //get and set AuthorName from wpdAuthorDemo.xml file        public string AuthorName { get; set; }        //get and set AuthorDescription from wpdAuthorDemo.xml file        public string AuthorDescription { get; set; }        //get and set AuthorImage from wpdAuthorDemo.xml file        public ImageSource AuthorImage { get; set; }        //get and set AuthorSource from wpdAuthorDemo.xml file        public Uri AuthorSource { get; set; }    }}
```

\

Now write down the following code for ‘MainPage.xaml’.

##### Code of ‘MainPage.xaml’:

```
<!--ContentPanel - place additional content here-->        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">            <ListBox Height="573" HorizontalAlignment="Left" Margin="12,28,0,0" Name="lstboxAuthors" VerticalAlignment="Top" Width="472" ItemsSource="{Binding}" Loaded="lstboxAuthors_Loaded">                <ListBox.ItemTemplate>                    <DataTemplate>                        <StackPanel Orientation="Horizontal">                            <Image  Source="{Binding AuthorImage}" Width="150" Stretch="Fill" HorizontalAlignment="Center" />                            <StackPanel Width="370">                                <TextBlock Text="{Binding AuthorName}" FontWeight="Bold" FontSize="22"  />                                <TextBlock Text="{Binding Description}" />                                <TextBlock Text="{Binding Source}" />                            </StackPanel>                        </StackPanel>                    </DataTemplate>                </ListBox.ItemTemplate>             </ListBox>        </Grid>    </Grid>
```

Code of ‘MainPage.xaml.cs’:

\

```
using System.Windows;using Microsoft.Phone.Controls;using System.Xml.Linq;using System.Linq;using System;using System.Windows.Media.Imaging; namespace xmlDemo{    public partial class MainPage : PhoneApplicationPage    {        // Constructor        public MainPage()        {            InitializeComponent();        }         private void lstboxAuthors_Loaded(objectsender, RoutedEventArgse)        {            var element= XElement.Load("wpdAuthorDemo.xml");            var authors=              from varinelement.Descendants("Author")              orderby var.Attribute("AuthorName").Value              select new Authors              {                  AuthorId=Convert.ToInt32(var.Attribute("AuthorId").Value),                  AuthorName=var.Attribute("AuthorName").Value,                  AuthorImage=GetImage(var.Attribute("AuthorImage").Value),                  AuthorDescription=var.Attribute("Description").Value,                  AuthorSource=new Uri(var.Attribute("Source").Value)              };            lstboxAuthors.DataContext=authors;        }        public BitmapImage GetImage(stringpath)        {            return new BitmapImage(new Uri(path, UriKind.Absolute));        }    }}
```

\

Now execute [your application](https://answers.mindstick.com/qa/95487/how-do-you-troubleshoot-when-your-application-link-is-not-responding); following window will be appearing.

\

![How to use XML in Windows Phone 7 Development](https://www.mindstick.com/mindstickarticle/23cd7317-5071-4386-b6cf-def945f802ef/images/cd48c7fd-f586-4eff-8cb4-dfaba877f501.png)

To see whole picture, just slide your screen and see your picture one by one.

![How to use XML in Windows Phone 7 Development](https://www.mindstick.com/mindstickarticle/23cd7317-5071-4386-b6cf-def945f802ef/images/f8dd1666-4184-4b85-acfc-436a62f4ab13.png)

This is the complete description on how to use XML file in windows phone 7 developments. I hope you will enjoy it. Thanks for reading this article.

\

---

Original Source: https://www.mindstick.com/articles/925/how-to-use-xml-in-windows-phone-7-development

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
