|
LINQ to XML - Displaying and Filtering Data from XML file in C# |
|
In this tutorial, we will show how we can use LINQ to parse an XML file and extract the data so that we can use it. LINQ is probably the easiest way of parsing an XML file and this tutorial will show how it can be done. We will also see how we can filter results from the XML file - selecting data we want by adding using the WHERE clause of the LINQ extension to the C# language.We start off by creating a sample XML file, or if you have an existing XML file you want to parse, use that. Our example XML file will look like this:
|
|
|
|
|
<?xml version="1.0" encoding="utf-8" ?>
<Persons>
<Person>
<Name>Paxton</Name>
<City>Munich</City>
<Age>29</Age>
</Person>
<Person>
<Name>Mike</Name>
<City>Orlando</City>
<Age>33</Age>
</Person>
<Person>
<Name>Ella</Name>
<City>LA</City>
<Age>13</Age>
</Person>
<Person>
<Name>Ingrid</Name>
<City>Oslo</City>
<Age>63</Age>
</Person>
</Persons> |
|
Next, we will add two buttons to our page, a text box and a Literal Control. The buttons will allow us to retrieve all data and filter, the text box will allow us to filter using a text string, and we will use the Literal to display the returned data. Our ASP page will look something like this:
|
<form id="form1" runat="server">
<asp:Button ID="butGetXML" runat="server" Text="Get All XML"
onclick="butGetXML_Click" /><br /><br />
Filter data by City:<br />
City: <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
<asp:Button ID="butFilterXML" runat="server" Text="Filter XML"
onclick="butFilterXML_Click" /><br /><br />
<asp:Literal ID="litXMLData" runat="server"></asp:Literal>
</form> |
Notice the event handlers for each of the buttons have already been created. We will look at those next. We will display all the data from the XML file with the following button: |
|
protected void butGetXML_Click(object sender, EventArgs e)
{
XDocument xmlDoc = XDocument.Load("XMLFile.xml");
var persons = from person in xmlDoc.Descendants("Person")
select new
{
Name = person.Element("Name").Value,
City = person.Element("City").Value,
Age = person.Element("Age").Value,
};
litXMLData.Text = "";
foreach (var person in persons)
{
litXMLData.Text = litXMLData.Text + "Name: " + person.Name + "<br />";
litXMLData.Text = litXMLData.Text + "City: " + person.City + "<br />";
litXMLData.Text = litXMLData.Text + "Age: " + person.Age +
"<br /><br />";
}
if (litXMLData.Text == "")
litXMLData.Text = "No Results.";
} |
This code will load in the XML file and then we declare a variable to load in all the data from the XML file. Next, we set the Literal Control to blank and then for each record that's been returned, we will input the results into the Literal Control. Finally, if there were no returned results and the Liter Control remained empty, we change that to display 'No Results'.With the other button, we do something similar but we include a WHERE clause so that we can filter the data:
|
|
|
protected void butFilterXML_Click(object sender, EventArgs e)
{
XDocument xmlDoc = XDocument.Load("XMLFile.xml");
var persons = from person in xmlDoc.Descendants("Person")
where person.Element("City").Value == txtCity.Text
select new
{
Name = person.Element("Name").Value,
City = person.Element("City").Value,
Age = person.Element("Age").Value,
};
litXMLData.Text = "";
foreach (var person in persons)
{
litXMLData.Text = litXMLData.Text + "Name: " + person.Name +
"<br />";
litXMLData.Text = litXMLData.Text + "City: " + person.City +
"<br />";
litXMLData.Text = litXMLData.Text + "Age: " + person.Age +
"<br /><br />";
}
if (litXMLData.Text == "")
litXMLData.Text = "No Results.";
}
|
The only difference here is that we now have added an extra line to select only the data items that match the text we input into the text box.This web application will run right now - both buttons will be active and will return data from the XML file. We can improve the implementation by adding a bit of AJAX to stop the whole page posting back. We add a ScriptManager and an UpdatePanel to the ASP page to accomplish this:
|
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="butGetXML" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:Button ID="butGetXML" runat="server" Text="Get All XML"
onclick="butGetXML_Click" /><br /><br />
Filter data by City:<br />
City: <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
<asp:Button ID="butFilterXML" runat="server" Text="Filter XML"
onclick="butFilterXML_Click" /><br /><br />
<asp:Literal ID="litXMLData" runat="server"></asp:Literal>
</ContentTemplate>
</asp:UpdatePanel>
</form> |
| Download sourcecode
|