You can also use use a StringWriter.

StringWriter sw = new StringWriter();
XmlTextWriter wtr = new XmlTextWriter( sw );
...
string xml = sw.ToString();

Adam..

PS - the method on MemoryStream is ToArray, not ToByteArray

adam..

-----Original Message-----
From: Unmoderated discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Jekke Bladt
Sent: Tuesday, October 26, 2004 12:26 PM
To: [EMAIL PROTECTED]
Subject: Re: [ADVANCED-DOTNET] Getting XML out of a memory stream.

Woo hoo! Thank you. I'll go try that now.

--Jekke

-----Original Message-----
From: Unmoderated discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Steve Johnson
Sent: Tuesday, October 26, 2004 1:22 PM
To: [EMAIL PROTECTED]
Subject: Re: [ADVANCED-DOTNET] Getting XML out of a memory stream.


On Tue, 26 Oct 2004 13:13:54 -0400, Jekke Bladt <[EMAIL PROTECTED]>
wrote:
> All--
>
> I have an XmlTextWriter that writes to a MemoryStream. Once the XML is
> written, I would like to retrieve it as a string. What is the best way
> to do this?
>
> Here is the code for writing:
>
> MemoryStream theStream = new MemoryStream();
> XmlTextWriter xtw = new XmlTextWriter(theStream, Encoding.UTF8);
> xtw.WriteStartElement(_Name);
> xtw.WriteAttributeString("type", this.GetType().ToString());
> xtw.WriteAttributeString("scope",
>         ConfigurationManager.ScopeName(this.Scope));
> xtw.WriteAttributeString("value", _Value);
> xtw.WriteEndElement(); // _Name
> xtw.Flush();

string theString =
System.Text.Encoding.UTF8.GetString(theStream.ToByteArray())

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com
Some .NET courses you may be interested in:

Essential .NET: building applications and components with C#
November 29 - December 3, in Los Angeles
http://www.develop.com/courses/edotnet

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to