Groups | Blog | Home
all groups > dotnet xml > october 2005 >

dotnet xml : Most efficient way for write only XML


David Thielen
10/7/2005 2:59:03 PM
Hi;

If I am creating an XML file, what is the most efficient way to create it?
All I can see is creating an XmlDocument, building it up, then writing it to
a Stream. But that means I have the entire DOM sitting in memory when I have
no need for it.

???

--
David Thielen
10/7/2005 5:37:09 PM
Again, thank you

--
thanks - dave


[quoted text, click to view]
Derek Harmon
10/7/2005 7:55:14 PM
[quoted text, click to view]

Clearly the winner would be XmlWriter (again, XmlWriter.Create( ) with a
suitable XmlWriterSettings profile in .NET 2.0, or XmlTextWriter will fit the
bill in many .NET 1.x scenarios.) It allows an application to emit XML serially
such as in the following example:

writer.WriteStartElement( "message");
writer.WriteStartElement( "nested");
writer.WriteElementString( "Hello World!");
writer.WriteEndElement( ); // nested.
writer.WriteEndElement( ); // message.

to produce the simple XML document:

<message>
<nested>Hello World!</nested>
</message>

(Note that any indenting is a function of the Indentation/Indent property on your
XmlTextReader or XmlWriterSettings.)

If the tree-like construction of XmlDocument is closer to your information model
then it doesn't need to be whistfully disregarded. The close match and simplicity
may save you performance elsewhere. However, if you're dealing with large
documents that can't be decomposed easily than you're right about the expense
incurred for having the tree build-up like that.


Derek Harmon

AddThis Social Bookmark Button