By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,306 Members | 1,657 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,306 IT Pros & Developers. It's quick & easy.

Can you use XMLTextWriter to dynamically generate and display XML?

P: n/a
I want to use the XMLTextWriter to build and xml stream and then display or
pass the xml to another method. I do not wish to persist the xml. I am
using the Memory stream to store the xml but I am having problems extracting
the xml from memory. Here is my code.

Any suggestions?

public static string WriteDataToMemory()
{
Stream m = new MemoryStream();
XmlTextWriter x = new XmlTextWriter(m, System.Text.Encoding.UTF8);
x.Formatting=Formatting.Indented;
x.WriteStartDocument();
x.WriteStartElement("Test");
x.WriteElementString("string","testing");
x.WriteEndElement();
x.WriteEndDocument();
x.Close();
m.Flush();
return (string)m.ToString();
Nov 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
This is the correct way to do it

Stream m = new MemoryStream();
XmlTextWriter x = new XmlTextWriter(m, System.Text.Encoding.UTF8);
x.Formatting = Formatting.Indented;
x.WriteStartDocument();
x.WriteStartElement("Test");
x.WriteElementString("string","testing");
x.WriteEndElement();
x.WriteEndDocument();
x.Flush();

m.Position = 0;
byte[] b = new byte[m.Length];
m.Read(b,0,(int)m.Length);
x.Close();
return System.Text.Encoding.UTF8.GetString(b,0,b.Length);

Cheers,
John Wadie
Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.