How to Add XML File in Your Silverlight Project
Add Xml File named as Employee.xml and write the code given below
<?xml version="1.0" encoding="utf-8" ?>
<Employee>
<customer id="10001" first="Aditya"
last="Tiwari" company="R4R">aditya@r4r.com</customer>
<customer id="10002" first="Harsh"
last="Singh" company="R4R Techsoft">harsh@r4r.com</customer>
<customer id="10003" first="Anil"
last="Yadav" company="Onlineexam">anil@r4r.com</customer>
<customer id="10004" first="Manoj"
last="Kumar" company="R4R">manoj@r4r.com</customer>
<customer id="10005" first="Rajesh"
last="Kumar" company="R4R Consultants">rajesh@r4r.com</customer>
</Employee>
MainPage.xaml
<UserControl x:Class="SilverlightApplication14.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<ListBox x:Name="EmployeeList" Width="400" Height="200"
SelectionChanged="EmployeeList_SelectionChanged" />
</Grid>
</UserControl>
MainPage.xaml.cs page
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 System.Xml;
namespace SilverlightApplication14
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
PopulateEmployeeList();
}
private void PopulateEmployeeList()
{
XmlReader reader = XmlReader.Create("Employee.xml");
reader.MoveToContent();
while (reader.Read())
{
if(reader.NodeType == XmlNodeType.Element && reader.Name == "customer")
{
EmployeeList.Items.Add(new ListBoxItem()
{
Content = reader.GetAttribute("last") +
", " + reader.GetAttribute("first") + " (" + reader.ReadInnerXml() + ")"
});
}
if
(reader.NodeType == XmlNodeType.EndElement && reader.Name == "Employee")
{
break;
}
}
reader.Close();
}
}
}
Run the Silverlight Application