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

Why isn't MemoryStream working with XmlTextWriter?

P: n/a
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
Share this Question
Share on Google+
4 Replies


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

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

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

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