Tuesday, 22 July 2014

Read XMl File in Silver Light Code

using System;
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.Xml;
using System.Linq;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Windows.Media.Imaging;
namespace LowIntegrationSLApp
{
public partial class MainPage : UserControl
{
string promotion = "";
string fastTrack = "";
double avgScore = 0.0;
List myEmployeeList = new List();
public MainPage()
{
// Required to initialize variables
InitializeComponent();
}
private void btnRefresh_Click(object sender, RoutedEventArgs e)
{
XElement employee = XElement.Load(@"Employee.xml");
resetThermometer();
string tempEmpName = "";
string tempEmpID = "";
string tempFY08 = "";
string tempFY09 = "";
string tempFY10 = "";
var employees =
from emp in employee.Elements("Employee")
select new
{
tempEmpName = (string)emp.Element("Name"),
tempEmpID = (string)emp.Element("EmpID"),
tempFY08 = (string)emp.Element("FY08"),
tempFY09 = (string)emp.Element("FY09"),
tempFY10 = (string)emp.Element("FY10")
};
foreach (var item in employees)
{
Employees tempEmployee = new Employees();
tempEmployee.empName = item.tempEmpName.ToString();
lstbxEmployeeNames.Items.Add(tempEmployee.empName);
tempEmployee.empID = item.tempEmpID.ToString();
tempEmployee.empFY08 = item.tempFY08.ToString();
tempEmployee.empFY09 = item.tempFY09.ToString();
tempEmployee.empFy10 = item.tempFY10.ToString();
myEmployeeList.Add(tempEmployee);
}
}
private void lstbxEmployeeNames_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
resetThermometer();
string tempEmpID = "";
string tempFY08 = "";
string tempFY09 = "";
string tempFY10 = "";
string empFilter = lstbxEmployeeNames.SelectedItem.ToString();
var expr =
from emp in myEmployeeList
select new
{
emp.empName,
emp.empID,
emp.empFY08,
emp.empFY09,
emp.empFy10
};
foreach (var item in expr)
{
if (item.empName == empFilter)
{
txtbxEmplID.Text = item.empID;
txtbxFY08.Text = item.empFY08;
txtbxFY09.Text = item.empFY09;
txtbxFY10.Text = item.empFy10;
}
}
}
private void btnCalc_Click(object sender, RoutedEventArgs e)
{
resetThermometer();
double rvwFY08 = Double.Parse(txtbxFY08.Text);
double rvwFY09 = Double.Parse(txtbxFY09.Text);
double rvwFY10 = Double.Parse(txtbxFY10.Text);
avgScore = Math.Round(((rvwFY08 + rvwFY09 + rvwFY10) / 3), 2) * 100 / 100;
if (avgScore >= 4.5)
{
promotion = "Yes";
fastTrack = "Yes";
shapeRectanglePromo.Height = 3;
}
else if (avgScore >= 4.0)
{
promotion = "Yes";
fastTrack = "No";
shapeRectangleNoPromo.Height = 3;
}
else
{
promotion = "No";
fastTrack = "No";
shapeRectangleLowScore.Height = 3;
}
txtbxPromo.Text = promotion;
txtbxFastTrack.Text = fastTrack;
txtbxAVGScore.Text = avgScore.ToString();
}
private void resetThermometer()
{
shapeRectanglePromo.Height = 0;
shapeRectangleNoPromo.Height = 0;
shapeRectangleLowScore.Height = 0;
}
}
}
//------------------
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 LowIntegrationSLApp
{
public class Employees
{
public string empName { get; set; }
public string empID { get; set; }
public string empFY08 { get; set; }
public string empFY09 { get; set; }
public string empFy10 { get; set; }
}
}
//-----------------
Xml
John Doe
837901
3.2
3.4
3.8
Kelly Jackson
983011
2.8
2.9
3.0
Sam Sheppard
10290
4.2
4.3
4.5
Lamont Smyth
129775
3.8
3.6
3.2
Beth Canyon
38921
2.1
2.2
2.0
Barry McCathry
201982
3.3
2.9
3.7
Steve Denn
290122
4.5
4.6
4.5
Ahmed Habul
0992812
3.9
3.8
3.9
view raw gistfile1.cs hosted with ❤ by GitHub