articles

Home / DeveloperSection / Articles / How to use XML in Windows Phone 7 Development

How to use XML in Windows Phone 7 Development

Anonymous User 9126 16-Apr-2012

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.

Getting started:

1.       Open Visual Studio

2.       Create new Silverlight Windows 7 Phone Development Project

3.       Enter your project Name

4.       Click on button ‘Ok’

How to use XML in Windows Phone 7 Development

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

<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 with ListBox control, 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; following window will be appearing.


How to use XML in Windows Phone 7 Development

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

How to use XML in Windows Phone 7 Development

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.



Updated 07-Sep-2019
I am a content writter !

Leave Comment

Comments

Liked By