469,613 Members | 2,114 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Why isn't MemoryStream working with XmlTextWriter?

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 will rehydrate my class. I've
built a little Windows forms test app to see if my
approach will work. It doesn't. When I step through the
code in the debugger, I see that the MemoryStream is
null, and the string variable "xmlFragment" is an empty
string. I'm mystified as to why. Here's the relevant
code snippet:

// write an XML fragment to a memory stream
MemoryStream myStream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(myStream, new
System.Text.UTF8Encoding());
writer.WriteStartElement("Quote");
writer.WriteAttributeString("Number", txtQuoteNum.Text);
writer.WriteAttributeString("Unit", txtUnit.Text ==
null ? String.Empty : txtUnit.Text);
writer.WriteAttributeString("Form", txtForm.Text ==
null ? String.Empty : txtForm.Text);
writer.WriteAttributeString("Wind", txtElig.Text ==
null ? String.Empty : txtElig.Text);
writer.WriteEndElement();

// convert the stream to a string
string xmlFragment = System.Text.Encoding.UTF8.GetString
(myStream.GetBuffer());

On my screen and in the debugger, I see that the Text
property of the various textboxes has real data. Yet all
this code writes zilch into the MemoryStream, and
therefore into the string. What gives? Is there a
better approach?

Thanks in advance!
Chris
Nov 11 '05 #1
4 11807
Hi,
you have do close the XmlTextWriter object bevor mystream.GetBuffer().

......
writer.WriteEndElement();

writer.Close(); // It works, I tried it out

// convert the stream to a string
string xmlFragment = System.Text.Encoding.UTF8.GetString
(myStream.GetBuffer());

Günter Schwaiger
www.schwaiger.at
"Chris" <ch*********@nospam.yahoo.com> schrieb im Newsbeitrag
news:08****************************@phx.gbl...
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 will rehydrate my class. I've
built a little Windows forms test app to see if my
approach will work. It doesn't. When I step through the
code in the debugger, I see that the MemoryStream is
null, and the string variable "xmlFragment" is an empty
string. I'm mystified as to why. Here's the relevant
code snippet:

// write an XML fragment to a memory stream
MemoryStream myStream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(myStream, new
System.Text.UTF8Encoding());
writer.WriteStartElement("Quote");
writer.WriteAttributeString("Number", txtQuoteNum.Text);
writer.WriteAttributeString("Unit", txtUnit.Text ==
null ? String.Empty : txtUnit.Text);
writer.WriteAttributeString("Form", txtForm.Text ==
null ? String.Empty : txtForm.Text);
writer.WriteAttributeString("Wind", txtElig.Text ==
null ? String.Empty : txtElig.Text);
writer.WriteEndElement();

// convert the stream to a string
string xmlFragment = System.Text.Encoding.UTF8.GetString
(myStream.GetBuffer());

On my screen and in the debugger, I see that the Text
property of the various textboxes has real data. Yet all
this code writes zilch into the MemoryStream, and
therefore into the string. What gives? Is there a
better approach?

Thanks in advance!
Chris

Nov 11 '05 #2
Calling the Flush method should have the same effect, if you don't want to
close the writer for any given reason.
"Günter Schwaiger" <office@Please_No_SPAMschwaiger.at> wrote in message
news:eP**************@TK2MSFTNGP10.phx.gbl...
Hi,
you have do close the XmlTextWriter object bevor mystream.GetBuffer().

.....
writer.WriteEndElement();

writer.Close(); // It works, I tried it out

// convert the stream to a string
string xmlFragment = System.Text.Encoding.UTF8.GetString
(myStream.GetBuffer());

Günter Schwaiger
www.schwaiger.at
"Chris" <ch*********@nospam.yahoo.com> schrieb im Newsbeitrag
news:08****************************@phx.gbl...
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 will rehydrate my class. I've
built a little Windows forms test app to see if my
approach will work. It doesn't. When I step through the
code in the debugger, I see that the MemoryStream is
null, and the string variable "xmlFragment" is an empty
string. I'm mystified as to why. Here's the relevant
code snippet:

// write an XML fragment to a memory stream
MemoryStream myStream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(myStream, new
System.Text.UTF8Encoding());
writer.WriteStartElement("Quote");
writer.WriteAttributeString("Number", txtQuoteNum.Text);
writer.WriteAttributeString("Unit", txtUnit.Text ==
null ? String.Empty : txtUnit.Text);
writer.WriteAttributeString("Form", txtForm.Text ==
null ? String.Empty : txtForm.Text);
writer.WriteAttributeString("Wind", txtElig.Text ==
null ? String.Empty : txtElig.Text);
writer.WriteEndElement();

// convert the stream to a string
string xmlFragment = System.Text.Encoding.UTF8.GetString
(myStream.GetBuffer());

On my screen and in the debugger, I see that the Text
property of the various textboxes has real data. Yet all
this code writes zilch into the MemoryStream, and
therefore into the string. What gives? Is there a
better approach?

Thanks in advance!
Chris


Nov 11 '05 #3
Chris wrote:
I'm stumped and could use some suggestions. I'm trying
to serialize some data to an XML fragment in string form,

btw, you can write directly to a string using StringWriter class, there is no
need for any interim buffers:
StringWriter sw = new StringWriter();
XmlTextWriter w = new XmlTextWriter(sw);
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #4
Gunter, Oleg, and Daniel -

Thanks for your suggestions, all of which helped. I
think using a StringWriter produces the simplest code, so
that's what I will use in production.

- Chris
-----Original Message-----
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 will rehydrate my class. I've
built a little Windows forms test app to see if my
approach will work. It doesn't. When I step through thecode in the debugger, I see that the MemoryStream is
null, and the string variable "xmlFragment" is an empty
string. I'm mystified as to why. Here's the relevant
code snippet:

// write an XML fragment to a memory stream
MemoryStream myStream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(myStream, new
System.Text.UTF8Encoding());
writer.WriteStartElement("Quote");
writer.WriteAttributeString("Number", txtQuoteNum.Text);
writer.WriteAttributeString("Unit", txtUnit.Text ==
null ? String.Empty : txtUnit.Text);
writer.WriteAttributeString("Form", txtForm.Text ==
null ? String.Empty : txtForm.Text);
writer.WriteAttributeString("Wind", txtElig.Text ==
null ? String.Empty : txtElig.Text);
writer.WriteEndElement();

// convert the stream to a string
string xmlFragment = System.Text.Encoding.UTF8.GetString
(myStream.GetBuffer());

On my screen and in the debugger, I see that the Text
property of the various textboxes has real data. Yet allthis code writes zilch into the MemoryStream, and
therefore into the string. What gives? Is there a
better approach?

Thanks in advance!
Chris


Nov 11 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by grs | last post: by
4 posts views Thread by Ashley Brewerton | last post: by
2 posts views Thread by Reshma Prabhu | last post: by
2 posts views Thread by Random | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.