472,791 Members | 956 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,791 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 6203
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: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.