In this recipe, let's look more into navigating the XML file. There are many ways to navigate. Here we will learn how to load the XML file directly and find specific information and return it using LINQ methods.
Let's add a new project called Recipe4_SearchXml
to our Ch3_Recipes
solution. Copy DataClass.cs
and MyTasks.xml
from the first recipe. Change the name of the page to SearchXml
.
In the following steps, let's create an application to get the priority of a task by navigating the XML document:
MyTasks.xml
we created earlier:<?xmlversion="1.0"encoding="utf-8" ?> <tasks> <taskname="name 1" notes="notes 1" priority="Low" datedue="03/01/2011" datecreated="02/01/2011"/> <taskname="name 2" notes="notes 2" priority="High" datedue="04/01/2011" datecreated="02/01/2011"/> <taskname="name 3" notes="notes 3" priority="Medium" datedue="05/01/2011" datecreated="02/01/2011"/> </tasks>
TextBox
control to search the name of the task and a Button
control inside the ContentPanel
grid:<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0" Grid.ColumnSpan="2"> <TextBox Height="72" HorizontalAlignment="Left" Margin="0,52,0,0" Name="textBox1" Text="" VerticalAlignment="Top" Width="460" /> <TextBlock Height="30" HorizontalAlignment="Left" Margin="14,32,0,0" Name="textBlock1" Text="Search Task Name" VerticalAlignment="Top" /> <Button Content="Get Priority" Height="72" HorizontalAlignment="Left" Margin="0,116,0,0" Name="button1" VerticalAlignment="Top" Width="197" Click="button1_Click" /> </Grid>
TextBlock
to display the results found in the search:<Grid x:Name=" ContentPane2" Grid.Row="1" Margin="12,216,12,0" Grid.ColumnSpan="2"> <TextBlock Height="156" HorizontalAlignment="Left" Margin="14,32,0,0" Name="txbResults" Text="search results" VerticalAlignment="Top" Width="425"FontSize="56" /> </Grid>
MainPage.xaml.cs
file and add a method named getPriority
with a parameter as the task name:// Sample showing how to navigate to a specific // node of the xml document Private string getPriority(stringtaskName) { XDocument doc = XDocument.Load(@"MyTasks.xml"); var tasks = doc.Document.Descendants(XName.Get("task")); string priority = null; foreach (var task in tasks) { if (task.Attribute(XName.Get("name")).Value == taskName) { priority = task.Attribute(XName.Get("priority")).Value; } } return priority; }
getPriority
method when the search button is clicked:Private void button1_Click(object sender, RoutedEventArgs e) { StringtxtSearch = textBox1.Text; this.txbResults.Text = getPriority(txtSearch); }
First we use the XDocument
to load the XML file MyTasks.xml
to a local variable. Then, we get all the task child elements using the Descendants
method. We loop through each of the task child elements and compare the attributes. If the name matches the Task Name we are looking for, then the Get Priority attribute value is returned.
For navigating the XML, we can also use the XPath
language and XPathNavigator
classes. For more information on this topic refer to MSDN:
http://msdn.microsoft.com/en-us/library/0ysh8kd0(v=vs.80).aspx