XmlTextWriter & MemoryStream

  • Thread starter Thread starter Random
  • Start date Start date
R

Random

Why, oh why, won't my XmlTextWriter write properly to the MemoryStream. Or,
why can't I read the xml back out of the MemoryStream! I can't think of a
thing....

Dim reader As XmlReader

Dim stream As MemoryStream = New MemoryStream()
Dim writer As XmlWriter = New XmlTextWriter(stream, Encoding.UTF8)

writer.WriteStartDocument(True)
writer.WriteStartElement("fooitems")
writer.WriteElementString("foo", "myfoo")
writer.WriteEndElement()
writer.WriteEndDocument()

reader = New XmlTextReader(stream)
writer.Close()

reader.MoveToContent()
reader.ReadOuterXml()

After I run this, I get a 'Root Element is missing' error. I can't see that
anything ever gets into the stream.
 
After you write to the stream and before you read from it, set the
Position back to 0.

HTH,

Sam
 
Thanks for the tip. I had been trying different scenarios with that as
well. Turns out I had to make three changes......

'after finishing the document with WriteEndDocument, I had to flush the
content to the stream, then reset the position
writer.Flush()
stream.Position = 0
'then, don't close the writer with Close()
 

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