XMLTextWrier to string??

  • Thread starter Thread starter TPS
  • Start date Start date
T

TPS

I need to go from an XMLTextWriter to a string!


I am using the XmlTextWriter class to build an xml document. I need the
ultimate
output to go to a string, so am using the MemoryStream class as the
underlying
stream for the writer. I can't figure out how to get the contents of the
MemoryStream into a string when I am finished. I attempted to use the
StreamReader object, but that got me an empty string. All the documentation
shows how to go to a byte array, and I suppose I could go from there, but
that
seems like a roundabout way to do what I would think would be a common task.

What do I need to do, or how does everyone else approach this?

Thanks
TPS
 
TPS said:
I need to go from an XMLTextWriter to a string!


StringWriter writer = new StringWriter();
XmlTextWriter xmlWriter = new XmlTextWriter(writer);
xmlWriter.WriteStartDocument(true);
xmlWriter.WriteStartElement("x");
xmlWriter.WriteEndDocument();

Console.WriteLine(writer.ToString());
 
StringWriter writer = new StringWriter();
XmlTextWriter xmlWriter = new XmlTextWriter(writer);
xmlWriter.WriteStartDocument(true);
xmlWriter.WriteStartElement("x");
xmlWriter.WriteEndDocument();
Console.WriteLine(writer.ToString());


Mickey, thanks for your reply....

but ..

writer.ToString() returns "XmlTextWriter object"

not the xml.

Am I missing something or are you actually able to get xml out of your
example?

Thanks again, looking forward to using this solution.

TPS
 
You should call ToString() on the StringWriter instance, not the
XmlTextWriter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top