
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
846
|
Chapter 15: XML
// Display XML.
Console.WriteLine("Generated XML:\r\n{0}",addrBook.OuterXml);
Console.WriteLine( );
}
This method gives the output like this:
Generated XML:
<AddressBook><Contact name="Tim" phone="999-888-0000" /><Contact name="Newman"
phone="666-666-6666" /><Contact name="Harold" phone="777-555-3333" /></AddressBook>
Both methods produce the same XML, but the first method is formatted with
indents.
Discussion
Now that you have seen two ways to do this, the question arises: “Which one to
use?” The
XmlDocument uses the traditional DOM method of interacting with XML,
while the
XmlReader/XmlWriter combination deals with XML in a stream. If you are
dealing with larger documents, you are probably better off using the
XmlReader/
XmlWriter combination than the XmlDocument. The XmlReader/XmlWriter combination
is the better-performing of the two when you do not need the whole document in
memory. If you need the power of being able to traverse back over what you have
written already or write items out of order, use
XmlDocument.
XmlDocument is the class that implements the DOM model for XML processing in
the .NET Framework. The DOM holds all of the nodes in the XML in memory at the
same time, which enables tree traversal both forward and backward. DOM also
allows for a writable interface ...