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:

  1. 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>
    
  2. In the XML file, we have each task as a child element under the parent element tasks. Each task element has attributes name, notes, priority, datedue, and datecreated.
  3. Add a 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>
    
  4. Add another 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>
    
  5. Open the 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;
    }
    
  6. Call the getPriority method when the search button is clicked:
    Private void button1_Click(object sender, RoutedEventArgs e)
    {
    StringtxtSearch = textBox1.Text;
    this.txbResults.Text = getPriority(txtSearch);
    }
    
  7. Press F5. Now search for task name name1 and you should get the result Low as the priority, as illustrated in the following screenshot:
    How to do it…

How it works...

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.

There's more...

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

See also

Check the recipe titled Object serialization to XML in this chapter to understand how you can serialize and deserialize objects to and from XML file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset