15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

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

916 ❘ ChaPTer 33 mAnipulAtinG xml<br />

After executing this code, you end up with the same functionality as in the previous example, but there<br />

is one additional book in the text box, The Case of the Missing Cookie (a soon-to-be classic). If you look<br />

closely at the code, you can see that this is actually a fairly simple process. The first thing that you do is<br />

create a new book element:<br />

XmlElement newBook = doc.CreateElement("book");<br />

CreateElement() has three overloads that allow you to specify the following:<br />

➤<br />

➤<br />

➤<br />

The element name<br />

The name <strong>and</strong> namespace URI<br />

The prefix, localname, <strong>and</strong> namespace<br />

Once the element is created, you need to add attributes:<br />

newBook.SetAttribute("genre","Mystery");<br />

newBook.SetAttribute("publicationdate","2001");<br />

newBook.SetAttribute("ISBN","123456789");<br />

Now that you have the attributes created, you need to add the other elements of a book:<br />

XmlElement newTitle = doc.CreateElement("title");<br />

newTitle.InnerText = "The Case of the Missing Cookie";<br />

newBook.AppendChild(newTitle);<br />

Once again, you create a new XmlElement-based object (newTitle). Then you set the InnerText property<br />

to the title of our new classic <strong>and</strong> append the element as a child to the book element. You repeat this for<br />

the rest of the elements in this book element. Note that you add the name element as a child to the author<br />

element. This will give you the proper nesting relationship, as in the other book elements.<br />

Finally, you append the newBook element to the doc.DocumentElement node. This is the same level as all of<br />

the other book elements. You have now updated an existing document with a new element.<br />

The last thing to do is to write the new XML document to disk. In this example, you create a new<br />

XmlTextWriter <strong>and</strong> pass it to the WriteContentTo() method. WriteContentTo() <strong>and</strong> WriteTo() both<br />

take an XmlTextWriter as a parameter. WriteContentTo() saves the current node <strong>and</strong> all of its children<br />

to the XmlTextWriter, whereas WriteTo() just saves the current node. Because doc is an XmlDocumentbased<br />

object, it represents the entire document <strong>and</strong> so<br />

that is what is saved. You could also use the Save()<br />

method. It will always save the entire document.<br />

Save() has four overloads. You can specify a string<br />

with the filename <strong>and</strong> path, a Stream-based object,<br />

a TextWriter-based object, or an XmlWriter-based<br />

object.<br />

You also call the Close() method on<br />

XmlTextWriter to flush the internal buffers <strong>and</strong><br />

close the file.<br />

Figure 33-1 shows what you get when you run this<br />

example. Notice the new entry at the bottom of<br />

the list.<br />

Earlier in the chapter, you saw how to create a<br />

document using the XmlTextWriter class. You can also use XmlDocument. Why would you use one in<br />

preference to the other If the data that you want streamed to XML is available <strong>and</strong> ready to write, then<br />

the XmlTextWriter class is the best choice. However, if you need to build the XML document a little at a<br />

time, inserting nodes into various places, then creating the document with XmlDocument might be the better<br />

choice. You can accomplish this by changing the following line:<br />

doc.Load("books.xml");<br />

figure 33-1<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!