How to Use XML in Windows Phone 7


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.


    Similar Articles