Reader Level:
ARTICLE

How to Use XML in Windows Phone 7

Posted by Raj Kumar Articles | Windows Phone February 09, 2011
This article is going to explain how to get data and show data from XML file using Windows Phone 7.
  • 1
  • 0
  • 29569


Getting Started

Creating a Windows Phone Application:

  • Open Visual Studio 2010.
  • Go to File => New => Project
  • Select Silverlight for Windows Phone from the Installed templates and choose Windows Phone Application
  • Enter the Name and choose the location.

  • Click OK.

    img1.jpg

    First of all add a new XML file using Add New Item tab.
    Authors.XML

    <?
    xml version="1.0" encoding="utf-8" ?>
    <Authors>
      <
    Author AuthorId="1"          
               AuthorName="Raj Kumar"
               Description="Michael Gold is President of Microgold Software Inc., makers of the WithClass UML Tool. His company is a Microsoft VBA Partner and Borland
    Partner. Mike is a Microsoft MVP and founding member of C# Corner. He has a BSEE and MEng EE from Cornell University and has consulted for Chase Manhattan Bank,
    JP Morgan, Merrill Lynch, and Charles Schwab. Currently he is a senior developer at Finisar Corp. He has been involved in several .NET book projects, and is currently
    working on a book for using .NET with embedded systems.
    "
               AuthorImage="Raj.jpg"
              Source="http://www.c-sharpcorner.com/Authors/AuthorDetails.aspx?AuthorId=raj1979" />
      <Author AuthorId="2"
               AuthorName="Mike Gold"
               Description="Michael Gold is President of Microgold Software Inc., makers of the WithClass UML Tool. His company is a Microsoft VBA Partner and Borland
    Partner. Mike is a Microsoft MVP and founding member of C# Corner. He has a BSEE and MEng EE from Cornell University and has consulted for Chase Manhattan Bank,
    JP Morgan, Merrill Lynch, and Charles Schwab. Currently he is a senior developer at Finisar Corp. He has been involved in several .NET book projects, and is currently
    working on a book for using .NET with embedded systems.
    "
               AuthorImage="Mike.jpg"
              Source="http://www.c-sharpcorner.com/Authors/AuthorDetails.aspx?AuthorId=mgold"></Author>          
      <Author AuthorId="3"
               AuthorName=" Mahesh Chand"
               Description="Mahesh is a software developer with over 13 years of experience building systems for Financial and Banking, Engineering and Architectural, Imaging, Construction, Biological and Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual
    Studio 2010, SQL Server, and Oracle. If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant
    in Philadelphia area or remote location.
    "
               AuthorImage="Mahesh.Jpg"
              Source="http://www.c-sharpcorner.com/Authors/AuthorDetails.aspx?AuthorId=mahesh" />
      <Author AuthorId="4"
               AuthorName="Destin joy"
               Description="Destin Joy is a Microsoft MVP in SharePoint server.He is also a Mind Cracker MVP.He has MCPD in SharePoint 2010,MCTS SharePoint 2010
    application development and MCTS in SharePoint 2007. He has expertise in core part of SharePoint (2010-2007) and passionate about new Microsoft technologies.
    Born and brought up in Kerala.
    "
               AuthorImage="Destin.Jpg"
              Source="http://www.c-sharpcorner.com/Authors/AuthorDetails.aspx?AuthorId=Roji.Joy" />
      <Author AuthorId="5"
               AuthorName="Dhananjay Kumar"
               Description="Dhananjay Kumar is Microsoft MVP  on connected system and works as Senior System Engineer for Infosys Technologies.
                                He lives in Pune , India.
    "
               AuthorImage="Dhananjay.jpg"
              Source="http://www.c-sharpcorner.com/Authors/AuthorDetails.aspx?AuthorID=dhananjaycoder" />
    </Authors>
    Now add a new class.
    Authors.cs

    public int AuthorId { get; set; }
    public string AuthorName { get; set; }
    public string Description { get; set; }
    public ImageSource AuthorImage { get; set; }
    public Uri Source { get; set
    ; }
    Now drag and drop listbox control on page.

    <!--ContentPanel - place additional content here-->
            <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
                <ListBox Height="553" ItemsSource="{Binding}" HorizontalAlignment="Left" Margin="6,23,0,0" Name="listBoxAuthors" VerticalAlignment="Top" Width="444"
    Loaded="listBox1_Loaded">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Image  Source="{Binding AuthorImage}" Width="150" Stretch="UniformToFill" 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>

    Code Behind.

    using System.Windows.Media.Imaging;
     
    // Constructor
            public MainPage()
            {
                InitializeComponent();
                SupportedOrientations = SupportedPageOrientation.PortraitOrLandscape;
            } 
      private void listBox1_Loaded(object sender, RoutedEventArgs e)
            {
                 var element = XElement.Load("Authors.xml");
                 var authors =
                 from var in element.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),
                     Description = var.Attribute("Description").Value,
                     Source = new Uri(var.Attribute("Source").Value)
                  }; 
                listBoxAuthors.DataContext = authors;
            } 
            public ImageSource GetImage(string path)
            {
                return new BitmapImage(new Uri(path, UriKind.Relative));
            }

    Now run the application.
    img2.jpg

    You can see rest of authors after scrolling
    img3.jpg

    We are done here, If questions or comments drop me a line in comments section.

    Article Extensions
    Contents added by srujana gudla on Jul 07, 2012
    Hi,

    I can able to display  the data but I am unable to display the image ,

    all the data from xml is displayed properly except image,

    If anybody know the answer please help me in this,

                                                                   

      private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
            {


                  string loginid = MainPage.name;



                ServiceReference2.EST_WebServicesSoapClient client = new ServiceReference2.EST_WebServicesSoapClient();
                client.GetEmployeeDetails_LoginIdCompleted += new EventHandler<ServiceReference2.GetEmployeeDetails_LoginIdCompletedEventArgs>(client_GetEmployeeDetails_LoginIdCompleted);
                client.GetEmployeeDetails_LoginIdAsync(loginid);

            }

            private void client_GetEmployeeDetails_LoginIdCompleted(object sender, ServiceReference2.GetEmployeeDetails_LoginIdCompletedEventArgs e)
            {
                XElement xmlNews = XElement.Parse(e.Result.ToString());


                Institute trubyte=new Institute();




                textBlock2.Text = "Welcome  " + xmlNews.Element("FirstName").Value;
      

    image1.Source = GetImage(xmlNews.Element("Staff_Image").Value);

                t.Text = xmlNews.Element("Name").Value;

                t5.Text = xmlNews.Element("Gender").Value;

                textBlock5.Text = xmlNews.Element("Age").Value;

                t2.Text = xmlNews.Element("SchoolName").Value;
                t1.Text = xmlNews.Element("SchoolTrackId").Value;
                t4.Text = xmlNews.Element("Contact_No").Value;
             t3.Text= xmlNews.Element("Email_Id").Value;

            }

            private void textBlock3_MouseEnter(object sender, MouseEventArgs e)
            {
                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
            }


            public ImageSource GetImage(string path)
            {
                return new BitmapImage(new Uri(path, UriKind.Relative));
            } 

    COMMENT USING

    Trending up