Wednesday, February 6, 2013

Windows phone Complex XML Parsing

I WAS WORKING WITH XML PARSING AND I FOUND A NICE WAY TO DO IT, Please have a look, for this purpose i used the link http://www.xmlfiles.com/examples/cd_catalog.xml which i found from net .

make a project and have a MainPage.xaml with following layout


<phone:PhoneApplicationPage 
    x:Class="Recipe4_RemoteXml.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="Remote Xml" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
         
            <Button Content="Go" Height="79" HorizontalAlignment="Left" Margin="12,6,0,0" Name="button1" VerticalAlignment="Top" Width="438" Click="button1_Click" />
        </Grid>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel2" Margin="12,217,12,-13" Grid.Row="1">
            <ListBox x:Name ="lstTasks" Margin="0,-126,0,0">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition />
                                <RowDefinition />
                                <RowDefinition Height="15" />
                            </Grid.RowDefinitions>

                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="150" />
                                <ColumnDefinition Width="200" />
                                <ColumnDefinition Width="100" />
                            </Grid.ColumnDefinitions>

                            <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding TITLE}" FontWeight="Bold" Foreground="OrangeRed"/>
                            <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding ARTIST}" />
                            <TextBlock Grid.Row="0" Grid.Column="2" Text="{Binding COUNTRY}"  Foreground="Yellow"/>
                            <TextBlock Grid.Row="1" Grid.ColumnSpan="3" Text="{Binding COMPANY}" />
                            <TextBlock Grid.Row="2" Grid.ColumnSpan="3" />
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>
    </Grid>
</phone:PhoneApplicationPage>


have a MainPage.xaml.cs with following code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

using System.IO;
using System.IO.IsolatedStorage;
using System.Xml;
using System.Xml.Linq;

namespace Recipe4_RemoteXml
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        WebClient remoteXml;
        public MainPage()
        {
            InitializeComponent();
            remoteXml = new WebClient();
            remoteXml.DownloadStringCompleted += new DownloadStringCompletedEventHandler(remoteXml_DownloadStringCompleted);
        }

        private void remoteXml_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            XDocument xdoc = XDocument.Parse(e.Result);
            IEnumerable<DataClass> iTasks;

            iTasks = from task in xdoc.Descendants("CD")
                     select new DataClass
                     {
                         TITLE = (string)task.Element("TITLE"),
                         ARTIST = (string)task.Element("ARTIST"),
                         COUNTRY = (string)task.Element("COUNTRY"),
                         COMPANY = (string)task.Element("COMPANY"),
                         PRICE = (string)task.Element("PRICE"),
                         YEAR = (string)task.Element("YEAR")

                     };
            lstTasks.ItemsSource = iTasks;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string txtUri = "http://www.xmlfiles.com/examples/cd_catalog.xml";
            txtUri = Uri.EscapeUriString(txtUri);
            Uri uri = new Uri(txtUri);

            remoteXml.DownloadStringAsync(uri);
        }
    }
}

the DataClass.cs code is given below


using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Recipe4_RemoteXml
{
    public class DataClass
    {
        public string TITLE { get; set; }
        public string ARTIST { get; set; }
        public string COUNTRY { get; set; }
        public string COMPANY { get; set; }
        public string PRICE { get; set; }
        public string YEAR { get; set; }

    }
}

the screen shot of the output is given below





No comments:

Post a Comment