473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

XmlTextWriter & MemoryStream

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
2 4088

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Chris | last post by:
Hi, I'm stumped and could use some suggestions. I'm trying to serialize some data to an XML fragment in string form, then deserialize it from a string back to an XML fragment, from which I...
2
by: Steve Gilbey | last post by:
I have a dataset from which I need to extract certain columns, format them into xml and output to an xmldocument object. From reading various posts on similar subjects, I have come up with the...
3
by: Dave | last post by:
Hi, I'm trying to create an XML document with XMLTextWriter but I want to store it in memory (not write it to a file like so many examples do). I tried the following but with no luck. Should I not...
2
by: vector | last post by:
Here is a function more or less exactly as I found it from somewhere on the internet. static string BeautifyXML(string sXML) { string result = ""; System.IO.MemoryStream ms = new...
1
by: SQLScott | last post by:
I have the following code that is supposed to use the XMLTextWriter to write the XML to a MemoryStream: Dim ms As MemoryStream ms = New MemoryStream Dim xtw As New Xml.XmlTextWriter(ms,...
2
by: Josh Newman | last post by:
I'm using the XMLTextWriter to create an XML document. I do not want the encoding attribure in the XML file. Instead of: <?xml version="1.0" encoding="utf-8"?> I want: <?xml version="1.0"?>...
3
by: Gustaf Liljegren | last post by:
I think I must use MemoryStream, but whenever I search for examples of MemoryStream, I get lots byte-per-byte reading, buffers and so on. It's hard to understand and seem overkill for my case...
8
by: Marc Gravell | last post by:
I want to write a method that will accept a stream as a parameter, and which will write xml to the stream (based in reality on database results) using the XmlTextWriter class. However, this insists...
3
by: GaryDean | last post by:
I'm using an XmlTextWriter and it's various methods such as WriteElementString, WriteStartElement, WriteEndElement, etc to create an xml document. When I instantiate the XmlTextWriter to a file......
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.