all groups > dotnet xml > may 2006 >
You're in the

dotnet xml

group:

Using XMLTextWriter (or related tool) to write to a string?


Using XMLTextWriter (or related tool) to write to a string? darrel
5/25/2006 2:13:01 PM
dotnet xml:
We've been using xmltextwriter to write XML files to the filesystem. For
various reasons, we're ditching the filesystem and want to store the XML as
text in a DB field.

Looking at all the examples of using the XMLTextWriter, however, it appears
that it is used solely for writing directly to a file or output stream. Is
there a way in ASP.net to use something like the XMLwriter to write valid
XML but just do it in memory and dump it into a string variable?

Or do I need to instead write the XML all by hand using a stringbuilder or
the like?

-Darrel

Re: Using XMLTextWriter (or related tool) to write to a string? Dhanvanth
5/25/2006 3:45:02 PM
You could pass in an StringWriter as a TextWriter to your XmlTextWriter
and after your writing you can extract it out of the StringWriter.

eg;

StringWriter sw=new StringWriter();

XmlTextWriter xtw=new XmlTextWriter(sw)

//Do your writing to XmlTextWriter here

String writtenXml = sw.ToString();

-Dhanvanth
Re: Using XMLTextWriter (or related tool) to write to a string? Oleg Tkachenko [MVP]
5/28/2006 12:00:00 AM
[quoted text, click to view]

Create XmlWriter that writes to StringWriter or MemoryStream. Beware
though that writing to StringWriter will always produce UTF-16 encoded
XML. If you need different encoding use MemoryStream.

--
Oleg Tkachenko [XML MVP, MCAD]
AddThis Social Bookmark Button