I n windows phone applications we can parse an xml document easily. we just need to follow few steps for this purpose make a project. and have a MaiPage.xml as follows
<phone:PhoneApplicationPage
x:Class="Recipe1_LinqToXml.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" Loaded="MainPage_Loaded">
<!--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="Linq To Xml" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Margin="12,0,12,-13" Grid.Row="1">
<ListBox x:Name ="lstTasks" Grid.Row="3" Grid.Column ="1">
<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 Name}" FontWeight="Bold" Foreground="OrangeRed"/>
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding DateDue}" />
<TextBlock Grid.Row="0" Grid.Column="2" Text="{Binding Priority}" Foreground="Yellow"/>
<TextBlock Grid.Row="1" Grid.ColumnSpan="3" Text="{Binding Notes}" />
<TextBlock Grid.Row="2" Grid.ColumnSpan="3" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Grid>
</phone:PhoneApplicationPage>
now copy the below given code to 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.IO;
using System.IO.IsolatedStorage;
using System.Xml;
using System.Xml.Linq;
namespace Recipe1_LinqToXml
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
// parseXMLUsingLinq("MyTasks.xml");
}
// Sample showing how to parse the XML using LINQ
private void parseXMLUsingLinq(string passedXmlFileName)
{
XDocument xdoc = XDocument.Load(passedXmlFileName);
IEnumerable<DataClass> iTasks;
iTasks = from task in xdoc.Descendants("task")
select new DataClass
{
Name = (string)task.Attribute("name"),
Notes = (string)task.Attribute("notes"),
Priority = (string)task.Attribute("priority"),
DateDue = (DateTime)task.Attribute("datedue"),
DateCreated = (DateTime)task.Attribute("datecreated")
};
lstTasks.ItemsSource = iTasks;
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
parseXMLUsingLinq("MyTasks.xml");
}
}
}
we need to make an another DataClass.cs with the code
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;
using System.Windows.Data;
using System.Runtime.Serialization;
namespace Recipe1_LinqToXml
{
public class DataClass
{
public string Name { get; set; }
public string Notes { get; set; }
public string Priority { get; set; }
public DateTime DateDue { get; set; }
public DateTime DateCreated { get; set; }
}
public class DateFormatter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
string formatString = parameter as string;
if (!string.IsNullOrEmpty(formatString))
{
return string.Format(culture, formatString, value);
}
return value.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
also make a MyTasks.xml with following xml
<?xml version="1.0" encoding="utf-8" ?>
<tasks>
<task name="name 1" notes="notes 1" priority="Low" datedue="03/01/2011" datecreated="02/01/2011"/>
<task name="name 2" notes="notes 2" priority="High" datedue="04/01/2011" datecreated="02/01/2011"/>
<task name="name 3" notes="notes 3" priority="Medium" datedue="05/01/2011" datecreated="02/01/2011"/>
</tasks>
after running the project on emulator you can see the outpust as below
<phone:PhoneApplicationPage
x:Class="Recipe1_LinqToXml.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" Loaded="MainPage_Loaded">
<!--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="Linq To Xml" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Margin="12,0,12,-13" Grid.Row="1">
<ListBox x:Name ="lstTasks" Grid.Row="3" Grid.Column ="1">
<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 Name}" FontWeight="Bold" Foreground="OrangeRed"/>
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding DateDue}" />
<TextBlock Grid.Row="0" Grid.Column="2" Text="{Binding Priority}" Foreground="Yellow"/>
<TextBlock Grid.Row="1" Grid.ColumnSpan="3" Text="{Binding Notes}" />
<TextBlock Grid.Row="2" Grid.ColumnSpan="3" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Grid>
</phone:PhoneApplicationPage>
now copy the below given code to 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.IO;
using System.IO.IsolatedStorage;
using System.Xml;
using System.Xml.Linq;
namespace Recipe1_LinqToXml
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
// parseXMLUsingLinq("MyTasks.xml");
}
// Sample showing how to parse the XML using LINQ
private void parseXMLUsingLinq(string passedXmlFileName)
{
XDocument xdoc = XDocument.Load(passedXmlFileName);
IEnumerable<DataClass> iTasks;
iTasks = from task in xdoc.Descendants("task")
select new DataClass
{
Name = (string)task.Attribute("name"),
Notes = (string)task.Attribute("notes"),
Priority = (string)task.Attribute("priority"),
DateDue = (DateTime)task.Attribute("datedue"),
DateCreated = (DateTime)task.Attribute("datecreated")
};
lstTasks.ItemsSource = iTasks;
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
parseXMLUsingLinq("MyTasks.xml");
}
}
}
we need to make an another DataClass.cs with the code
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;
using System.Windows.Data;
using System.Runtime.Serialization;
namespace Recipe1_LinqToXml
{
public class DataClass
{
public string Name { get; set; }
public string Notes { get; set; }
public string Priority { get; set; }
public DateTime DateDue { get; set; }
public DateTime DateCreated { get; set; }
}
public class DateFormatter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
string formatString = parameter as string;
if (!string.IsNullOrEmpty(formatString))
{
return string.Format(culture, formatString, value);
}
return value.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
also make a MyTasks.xml with following xml
<?xml version="1.0" encoding="utf-8" ?>
<tasks>
<task name="name 1" notes="notes 1" priority="Low" datedue="03/01/2011" datecreated="02/01/2011"/>
<task name="name 2" notes="notes 2" priority="High" datedue="04/01/2011" datecreated="02/01/2011"/>
<task name="name 3" notes="notes 3" priority="Medium" datedue="05/01/2011" datecreated="02/01/2011"/>
</tasks>
after running the project on emulator you can see the outpust as below
No comments:
Post a Comment