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

XmlTextWriter & MemoryStream

P: n/a
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.



Jan 25 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a

After you write to the stream and before you read from it, set the
Position back to 0.

HTH,

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Thu, 25 Jan 2007 14:26:51 -0800, "Random" <ci*******@hotmail.com>
wrote:
>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.



Jan 25 '07 #2

P: n/a
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()

"Samuel R. Neff" <sa********@nomail.comwrote in message
news:vr********************************@4ax.com...
>
After you write to the stream and before you read from it, set the
Position back to 0.

HTH,

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Thu, 25 Jan 2007 14:26:51 -0800, "Random" <ci*******@hotmail.com>
wrote:
>>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.




Jan 25 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.