469,593 Members | 2,105 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,593 developers. It's quick & easy.

Can you use XMLTextWriter to dynamically generate and display XML?

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
1 1778
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.

Similar topics

5 posts views Thread by Jain, Pranay Kumar | last post: by
2 posts views Thread by Jesper Stocholm | last post: by
2 posts views Thread by grawsha2000 | last post: by
4 posts views Thread by quest | last post: by
2 posts views Thread by fmancina | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.