1) Create New WPF Applictaion
2) Add Order class
FileName:Order.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WpfApplication1
{
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; }
}
}
}
2) Add Order class
FileName:Order.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WpfApplication1
{
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; }
}
}
}
3) Add Label and DataGrid to MainWindow.xaml
<Label Content="XML Parsing in WPF" Height="28" HorizontalAlignment="Left" Margin="10,10,0,0" Name="label1" VerticalAlignment="Top" />
<DataGrid AutoGenerateColumns="True" Height="200" HorizontalAlignment="Left" Margin="10,48,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="471" />
4) Add following code to MainWindows.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
List<Order> ordList = new List<Order>();
public MainWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
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>
";
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);
}
}
dataGrid1.ItemsSource = ordList;
}
}
}
}
Step 5) Run the Application/F5
output: