473,408 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

XmlTextWriter to StreamWriter problem

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

Similar topics

5
by: Jain, Pranay Kumar | last post by:
Hello Everyone, I have written a simple app. that converts the dataset into excelspreadsheet. The App. uses the following architecture. First it generates the dataset with corresponding...
4
by: Gauthier | last post by:
Hi, I've a simple issue with the use of extension objects. I'm trying to call a text formating method from an object that I add to my arguments collection, this method take an input string and...
3
by: Pete | last post by:
I'm trying to read an XML document and write out a slightly modified version using the XmlTextWriter. I'm basically trying to copy all the nodes exactly as they are read and do some text...
1
by: Riko Eksteen | last post by:
Hi I'm reading an xml file into an XmlDocument, adding some nodes, and writing it back out. I would like the nodes I add to assume the same level of indeting as the rest of the document. (I load...
2
by: Steve | last post by:
I'm an XML newb. I'm serializing a class and when I inspect the xml file, all the data is on one line rather than being nested and indented Is that normal? <code> StreamWriter sw = new...
1
by: Terrence Chan | last post by:
Does anyone know the size limit of the StringWriter and XmlTextWriter. Dim buffer As New System.IO.StringWriter Dim writer As New System.Xml.XmlTextWriter(buffer) Please point me to some...
4
by: John Salerno | last post by:
I thought I might use the XML functions in C# to help me do some repetitive typing in an XHTML file, but I'm stuck. Here's what I have before I just stopped: void WriteXMLFile() { string path...
4
by: quest | last post by:
Is there anyway I can generate the xml in the following format using XmlTextWriter ? Intended output: <?xml version="1.0" ?> I tried: XmlTextWriter xmlWriter = new...
4
by: Heron | last post by:
Hi, Could someone explain me why the following code doesn't work? The memorystream always remains with length 0. MemoryStream input = new MemoryStream();
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.