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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

560 ❘ CHAPTER 24 XML<br />

Shade<br />

<br />

... Other Plant elements omitted ...<br />

<br />

Follow these steps to prepare to generate random plant names.<br />

a. Place a TextBox on the form so that the user can enter the number of Plant elements<br />

to create.<br />

b. Place a multiline TextBox on the form. Fill it with a few hundred words from<br />

www.lipsum.com/feed/html.<br />

c. Get the text from the TextBox and convert it to lowercase.<br />

d. Use regular expressions to remove periods and commas from the text.<br />

e. Use string.Split to split the text into words using spaces, \r, and \n as word<br />

separators, removing empty entries.<br />

The following code shows two methods you can use to create random plant names and<br />

light values.<br />

// Return a random name.<br />

private Random Rand = new Random();<br />

private string RandomName(string[] words)<br />

{<br />

return<br />

words[Rand.Next(0, words.Length)] + " " +<br />

words[Rand.Next(0, words.Length)] + " " +<br />

words[Rand.Next(0, words.Length)];<br />

}<br />

// Return a random light value.<br />

private string RandomLight()<br />

{<br />

string[] values =<br />

{<br />

"Shade", "Partial Shade", "Partial Sun", "Full Sun"<br />

};<br />

return values[Rand.Next(0, values.Length)];<br />

}<br />

The program should pass the array of words it built during preparation into the RandomName<br />

method.<br />

2. Modify the program you wrote for Exercise 1 so that it uses the DOM instead of an<br />

XmlTextWriter. Which version of the program do you prefer Are there situations in which<br />

one would be better than the other<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!