03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

718 ❘ APPENDIX A Solutions to Exercises<br />

Find Name elements that have parents that are Plant elements, and the Plant has a Zone<br />

child with the value 1.<br />

With this new query, the program needs to loop through the selected Name elements and<br />

display their values. The previous query required the program to start at Plant elements<br />

and find their Name elements. (Both versions of the query are in the example program, so<br />

you can experiment with them.)<br />

When the user selects a plant from the ListBox, the program uses the following code to<br />

display information about the plant.<br />

// Display this plant's details.<br />

private void plantsListBox_SelectedIndexChanged(object sender, EventArgs e)<br />

{<br />

// Get the selected name.<br />

string plantName = plantsListBox.SelectedItem.ToString();<br />

// Find the Plant with a Name child that has this value.<br />

string query = "//Plant[Name=\"" + plantName + "\"]";<br />

XElement plant = Document.XPathSelectElement(query);<br />

}<br />

// Display the Plant element's data.<br />

detailsTextBox.Text = plant.ToString();<br />

This code composes a query similar to the following.<br />

//Plant[Name="id euismod pulvinar"]<br />

It then calls XPathSelectElement to select the first Plant element with the selected name.<br />

The code then displays that element’s data.<br />

6. The TransformPlants example program does this. The following code shows the XSLT file.<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

Name<br />

Zone<br />

Light<br />

<br />

<br />

<br />

<br />

<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!