Most efficient way for write only XML
Question posted by: David Thielen
(Guest)
on
November 12th, 2005 05:08 AM
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.
???
--
thanks - dave
2
Answers Posted
"David Thielen" <thielen@nospam.nospam> wrote in message news:D8E580D3-551F-4DF1-BB3F-9D89689BFA70@microsoft.com...[color=blue]
> If I am creating an XML file, what is the most efficient way to create it?[/color]
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
Again, thank you
--
thanks - dave
"Derek Harmon" wrote:
[color=blue]
> "David Thielen" <thielen@nospam.nospam> wrote in message news:D8E580D3-551F-4DF1-BB3F-9D89689BFA70@microsoft.com...[color=green]
> > If I am creating an XML file, what is the most efficient way to create it?[/color]
>
> 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
>
>
>[/color]
|
|
|
What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 196,828 network members.
Top Community Contributors
|