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

XmlTextWriter to StreamWriter problem

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

XmlTextWriter mytw = new XmlTextWriter("c:\\temp\\myfile.xml", null)

Everything writes properly and a good XML document gets created.

However if I instantiate the XmlTextWriter to a memoryStream...

Stream myMemoryStream = new MemoryStream();
TextWriter myStreamWriter = new StreamWriter(myMemoryStream);
XmlTextWriter mytw = new XmlTextWriter(myStreamWriter);

and then examine myMemoryStream, the last endelement</mydocdoes not get
written and after a mytw.Flush() it still isn't written. Then after a
mytw.Close() the mytw.basestream and myMemoryStream are null.

So writing to a file is successful because the end tag gets written to disk,
apparently by the Close() that also nulls the stream.

So, how can I get that last tag written and have access to the stream before
it is destroyed?

--
Regards,
Gary Blakely
Apr 7 '07 #1
Share this Question
Share on Google+
3 Replies


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

XmlTextWriter mytw = new XmlTextWriter("c:\\temp\\myfile.xml", null)

Everything writes properly and a good XML document gets created.

However if I instantiate the XmlTextWriter to a memoryStream...

Stream myMemoryStream = new MemoryStream();
TextWriter myStreamWriter = new StreamWriter(myMemoryStream);
XmlTextWriter mytw = new XmlTextWriter(myStreamWriter);

and then examine myMemoryStream, the last endelement</mydocdoes not get
written and after a mytw.Flush() it still isn't written. Then after a
mytw.Close() the mytw.basestream and myMemoryStream are null.

So writing to a file is successful because the end tag gets written to disk,
apparently by the Close() that also nulls the stream.

So, how can I get that last tag written and have access to the stream before
it is destroyed?
If you have the necessary WriteEndElement() and/or WriteEndDocument()
calls then I don't see a problem and have never experienced one.
Can you post example code where the closing tag is missing?

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Apr 8 '07 #2

P: n/a
Hi Gary,
Thanks for Martin's reply.

Frankly, I haven't met such issue before. I agree with Martin, your code
snippet will help on research very much. Would you please post some example
code where the closing tag is missing?

Additionally, there is a sample about how to Read XmlTextWriter to the
memory stream instead writing to the file. Hope it helps.
http://www.artisticode.com/Articles/45.aspx
[READ XMLTEXTWRITER TO MEMORYSTREAM]

Have a great day,
Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Apr 9 '07 #3

P: n/a
Hi Gary,

I just want to check if you have resolved the issue now.
If it still persists, please feel free to update here and we will follow
up. I'm glad to assist you.

Have a great day,
Sincererly,
Wen Yuan
Microsoft Online Community Support

Apr 11 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.