472,372 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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

Similar topics

2
by: Chris Lacey | last post by:
Is anyone aware why the following code (intended to write XML into a memory-based XmlTextWriter, and return the complete document as a string) produces badly formed XML due to the resultant string...
2
by: Jackfan | last post by:
I'm using XmlTextWriter now, but it seems it can just be used to create a xml file, how can I do? Thanks in advance! Jackfan
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,...
3
by: grs | last post by:
I am using some Microsoft examples that: 1. Serialize an object using XmlSerializer and write a file out to the harddrive. 2. Read back in the file using XmlDocument.Load and populate a string. ...
4
by: Ashley Brewerton | last post by:
When I use the DataSet.WriteXml using a XmlTextWriter to a MemoryStream, when I try to read the stream back into a DatasSet using ReadXml I get an error indicating the root not is not avaialable. ...
2
by: Reshma Prabhu | last post by:
hello, I am trying to do an xsl tranformation from an XML file into another xml file. I want the output file to be in MemoryStream so that my dataset can direclty read xml using...
3
by: rasx | last post by:
I get this error when I fail to ‘clean up’ my UTF-8 MemoryStream: “Invalid at the top level of the document. Error processing resource…” Visual Studio 2005 in debug mode shows well formed...
2
by: Random | last post by:
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...
4
by: | last post by:
Hi all, I want to create a method that does the following: 1) Programmatically instantiate a new XmlDataSource control 2) For each file in a named directory, make a "FileSystemItem" element 3)...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

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.