Step1) Create Silverlight Applicatication using .NET 4.0
Step 2) Add new Order class in Silverlight Project.
File Name: Order.cs
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;
Step 2) Add new Order class in Silverlight Project.
File Name: Order.cs
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 SilverlightApplication3
{
public class Order
{
private string OrderID, Amount, Qty, Product,Total;
public string PRODUCT
{
get { return Product; }
set { Product = value; }
}
public string ORDERID
{
get { return OrderID; }
set { OrderID = value; }
}
public string AMOUNT
{
get { return Amount; }
set { Amount = value; }
}
public string QTY
{
get { return Qty; }
set { Qty = value; }
}
public string TOTAL
{
get { return Total; }
set { Total = value; }
}
}
}
Step 3) Drag & Drop Label and DataGrid in MainPage.xaml
XAML excert
<Grid x:Name="LayoutRoot" Background="White">
<sdk:DataGrid AutoGenerateColumns="True" Height="156" HorizontalAlignment="Left" Margin="10,51,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="390" />
<sdk:Label Height="58" HorizontalAlignment="Left" FontSize="16" Margin="10,10,0,0" Name="label1" VerticalAlignment="Top" Width="378" Content="Silverlight XML Source in DataGrid" Foreground="#FFBE2C2C" FontWeight="Bold" FontStyle="Italic" />
</Grid>
Step 4) Open MainPage.XAML.cs
Add 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;
namespace SilverlightApplication3
{
public partial class MainPage : UserControl
{
List<Order> ordList = new List<Order>(); //Declare list of Orders
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
//Declare inline XML or it can be generic handler emiting xml from other website
String xmlString =
@"<?xml version='1.0'?>
<Orders>
<Order>
<ID>1</ID>
<Product>Cloths</Product>
<Amount>1500</Amount>
<Qty>10</Qty>
</Order>
<Order>
<ID>2</ID>
<Product>Shoes</Product>
<Amount>15000</Amount>
<Qty>20</Qty>
</Order>
</Orders>
";
//Read xml--> element by element
//Samething can be done using LINQ to XML.
//Silverlight doesnot support XPathNavigator.
System.Text.StringBuilder output = new System.Text.StringBuilder();
using (System.Xml.XmlReader reader =
System.Xml.XmlReader.Create(new System.IO.StringReader(xmlString)))
{
while (reader.Read())
{
Order o = new Order();
reader.ReadToFollowing("ID");
o.ORDERID = reader.ReadInnerXml();
//output.Append("ID=" + reader.ReadInnerXml());
reader.ReadToFollowing("Product");
o.PRODUCT = reader.ReadInnerXml();
//output.Append("Product=" + reader.ReadInnerXml());
reader.ReadToFollowing("Amount");
o.AMOUNT = reader.ReadInnerXml();
//output.Append("Amount=" + reader.ReadInnerXml());
reader.ReadToFollowing("Qty");
o.QTY = reader.ReadInnerXml();
//output.Append("Qty=" + reader.ReadInnerXml());
if (!String.IsNullOrEmpty(o.AMOUNT))
{
o.TOTAL = (Int16.Parse(o.AMOUNT) * Int16.Parse(o.QTY)).ToString();
ordList.Add(o); //Add to OrderList
}
}
dataGrid1.ItemsSource = ordList; //Assign it to DataGrid Item Source
}
}
}
}
Step 5) Run the Applictaion/F5
OUTPUT
No comments:
Post a Comment