In this article we will discuss how to easily Read, Write and Search Data in a XML File. Here we use two buttons, the first button will show XML file data in a ListBox like this:
And when we click on the Search Name Button:
Step 1
First we create two XML files like this:
Student.xml
<?xml version="1.0" encoding="utf-8" ?>
<school>
<student>
<firstname>Mahak</firstname>
<lastname>Gupta</lastname>
<age>27</age>
</student>
<student>
<firstname>Khyati</firstname>
<lastname>Jindal</lastname>
<age>5</age>
</student>
<student>
<firstname>Megha</firstname>
<lastname>Varshney</lastname>
<age>27</age>
</student>
<student>
<firstname>Meghal</firstname>
<lastname>Dani</lastname>
<age>22</age>
</student>
</school>
StudentData.xml
<?xml version="1.0" encoding="utf-8" ?>
<School>
<Student
FirstName="Mahak"
LastName="Gupta"
Age="27" />
<Student
FirstName="Khyati"
LastName="Jindal"
Age="5" />
<Student
FirstName="Megha"
LastName="Varshney"
Age="27" />
<Student
FirstName="Meghal"
LastName="Dani"
Age="22" />
</School>
Step 2
Now we will write the code in our .xaml page like this:
<Grid x:Name="ContentPanel" Grid.Row="2" >
<Button Content="Show data" Height="72" Name="btnShowData" Width="190" Click="button1_Click" Margin="12,6,278,528" />
<Button Content="Search Name" Height="72" Name="btnSearchName" Click="btnSearchname_Click" Margin="260,6,0,528" />
<ListBox x:Name="lstShow" FontFamily="Arial Black" VerticalAlignment="Center" Width="250">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="10" >
<TextBlock Text="{Binding FirstName}"/>
<TextBlock Text="{Binding LastName}"/>
<TextBlock Text="{Binding Age}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0" />
<GradientStop Color="#FFD42E2E" Offset="1" />
</LinearGradientBrush>
</ListBox.Background>
</ListBox>
<ListBox x:Name="lstSearchName" FontFamily="Arial Black" VerticalAlignment="Center" Width="250">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="20" >
<TextBlock Text="{Binding FirstName}"/>
<TextBlock Text="{Binding LastName}"/>
<TextBlock Text="{Binding Age}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0" />
<GradientStop Color="#FFD42E2E" Offset="1" />
</LinearGradientBrush>
</ListBox.Background>
</ListBox>
<TextBox Height="72" HorizontalAlignment="Left" Margin="12,124,0,0" Name="txtSearch" Text="" VerticalAlignment="Top" Width="300" Visibility="Collapsed" />
<Button Content="Search" Height="72" HorizontalAlignment="Left" Margin="308,124,0,0" Name="btnSearch" VerticalAlignment="Top" Width="160" Click="btnSearch_Click" Visibility="Collapsed" />
</Grid>
The output will be:
Step 3
Now we will write the code for the "Show data" button:
private void button1_Click(object sender, RoutedEventArgs e)
{
lstShow.Visibility = Visibility.Visible;
txtSearch.Visibility = Visibility.Collapsed;
btnSearch.Visibility = Visibility.Collapsed;
lstSearchName.Visibility = Visibility.Collapsed;
XDocument myData = XDocument.Load("Student.xml");
var data = from query in myData.Descendants("student")
select new Student
{
FirstName = (string)query.Element("firstname"), LastName = (string)query.Element("lastname"), Age = (int)query.Element("age")
};
lstShow.ItemsSource = data;
}
Here we create a class like this:
public class Student
{
string firstname;
string lastname;
int age;
public string FirstName
{
get { return firstname; }
set { firstname = value; }
}
public string LastName
{
get { return lastname; }
set { lastname = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
So the output will be:
Step 4
Now we will write the code for the "Search name" button, so when we click on this button a TextBox (txtSearch) and a Search button will be shown like this:
private void btnSearchname_Click(object sender, RoutedEventArgs e)
{
txtSearch.Visibility = Visibility.Visible;
btnSearch.Visibility = Visibility.Visible;
lstShow.Visibility = Visibility.Collapsed;
}
Now we will write the code for the Search Button like this:
private void btnSearch_Click(object sender, RoutedEventArgs e)
{
XDocument LoadStudentData = XDocument.Load("StudentData.xml");
var SearchData = from c in LoadStudentData.Descendants("Student")
where c.Attribute("FirstName").Value == txtSearch.Text.ToString()
select new Student()
{
FirstName = c.Attribute("FirstName").Value,
LastName = c.Attribute("LastName").Value
};
lstSearchName.ItemsSource = SearchData;
lstSearchName.Visibility = Visibility.Visible;
}
The output will be: