we can parse an xml document easily in windows phone applications. for this purpose an example is shown and the code is given below.
make a project with name XMLParsing_Example
1) MainPage.xaml
<phone:PhoneApplicationPage
x:Class="XMLParsing_Example.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" VerticalContentAlignment="Center" VerticalAlignment="Top">
<!--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-->
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Button x:Name="myButton" Grid.Row="0" Height="75" Content="Click forXML Parsing " VerticalAlignment="Top" />
<ListBox Name="lstEmployee" Grid.Row="2" FontSize="24">
</ListBox>
</Grid>
</Grid>
<!--Sample code showing usage of ApplicationBar-->
<!--<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem Text="MenuItem 1"/>
<shell:ApplicationBarMenuItem Text="MenuItem 2"/>
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>-->
</phone:PhoneApplicationPage>
2) MainPage.xaml.cs
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.Xml.Linq;
using System.IO;
using System.Diagnostics;
namespace XMLParsing_Example
{
public class Student
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime Dob { get; set; }
}
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
myButton.Click += new RoutedEventHandler(myButton_Click);
}
void myButton_Click(object sender, RoutedEventArgs e)
{
// pretend this came from a web service
string xmlData =
@"<rootdata>
<Student firstname='Jitesh' lastname='upadhyay' dob='01/01/2010 12:21:00'/>
<Student firstname='Jeetu' lastname='upadhyay' dob='01/01/2010 12:20:30' />
</rootdata>
";
XDocument dataDoc = XDocument.Load(new StringReader(xmlData));
var studentdata = from students in dataDoc.Descendants("Student")
let stamp = DateTime.Parse(students.Attribute("dob").Value)
orderby stamp ascending
select new Student
{
FirstName = students.Attribute("firstname").Value,
LastName = students.Attribute("lastname").Value
};
foreach (Student student in studentdata)
{
lstEmployee.Items.Add(student.FirstName + " " + student.LastName + " " + student.Dob);
}
}
}
}
3) the out put will be desirable after clicking the button "Click forXML Parsing "
make a project with name XMLParsing_Example
1) MainPage.xaml
<phone:PhoneApplicationPage
x:Class="XMLParsing_Example.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" VerticalContentAlignment="Center" VerticalAlignment="Top">
<!--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-->
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Button x:Name="myButton" Grid.Row="0" Height="75" Content="Click forXML Parsing " VerticalAlignment="Top" />
<ListBox Name="lstEmployee" Grid.Row="2" FontSize="24">
</ListBox>
</Grid>
</Grid>
<!--Sample code showing usage of ApplicationBar-->
<!--<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem Text="MenuItem 1"/>
<shell:ApplicationBarMenuItem Text="MenuItem 2"/>
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>-->
</phone:PhoneApplicationPage>
2) MainPage.xaml.cs
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.Xml.Linq;
using System.IO;
using System.Diagnostics;
namespace XMLParsing_Example
{
public class Student
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime Dob { get; set; }
}
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
myButton.Click += new RoutedEventHandler(myButton_Click);
}
void myButton_Click(object sender, RoutedEventArgs e)
{
// pretend this came from a web service
string xmlData =
@"<rootdata>
<Student firstname='Jitesh' lastname='upadhyay' dob='01/01/2010 12:21:00'/>
<Student firstname='Jeetu' lastname='upadhyay' dob='01/01/2010 12:20:30' />
</rootdata>
";
XDocument dataDoc = XDocument.Load(new StringReader(xmlData));
var studentdata = from students in dataDoc.Descendants("Student")
let stamp = DateTime.Parse(students.Attribute("dob").Value)
orderby stamp ascending
select new Student
{
FirstName = students.Attribute("firstname").Value,
LastName = students.Attribute("lastname").Value
};
foreach (Student student in studentdata)
{
lstEmployee.Items.Add(student.FirstName + " " + student.LastName + " " + student.Dob);
}
}
}
}
3) the out put will be desirable after clicking the button "Click forXML Parsing "
No comments:
Post a Comment