Navigating the XML file
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.
Getting ready
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
.
How to do it…
In the following steps, let's create an application to get the priority of a task by navigating the XML document:
Let's use the
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>
In the XML file...