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 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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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
|
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,...
|
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.
...
|
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. ...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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++...
|
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...
|
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...
|
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...
| |