473,795 Members | 2,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XmlTextWriter to StreamWriter problem

I'm using an XmlTextWriter and it's various methods such as
WriteElementStr ing, WriteStartEleme nt, WriteEndElement , etc to create an xml
document. When I instantiate the XmlTextWriter to a file...

XmlTextWriter mytw = new XmlTextWriter(" c:\\temp\\myfil e.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(my MemoryStream);
XmlTextWriter mytw = new XmlTextWriter(m yStreamWriter);

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 6271
GaryDean wrote:
I'm using an XmlTextWriter and it's various methods such as
WriteElementStr ing, WriteStartEleme nt, WriteEndElement , etc to create an xml
document. When I instantiate the XmlTextWriter to a file...

XmlTextWriter mytw = new XmlTextWriter(" c:\\temp\\myfil e.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(my MemoryStream);
XmlTextWriter mytw = new XmlTextWriter(m yStreamWriter);

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 WriteEndDocumen t()
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
9173
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 datatables(each datatable represents a worksheet).It uses an Xslt file to do the transformation of the xml (generate from dataset) and write the output xml into a xmltextwriter. The App converts the data into excel with almost all of the formatting
4
1983
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 output the formatted string. So far everything process correctly (the method is called without any issue) but my problem is that the output is htmlencoded, it means that my method (wich work as intended in other contexts) is called but the...
3
4200
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 manipulation on #text nodes of only certain named elements. example while (xmltextreader.Read()) { if (xmltextreader.NodeType == XmlNodeType.Text)
1
10224
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 the document with PreserveWhiteSpace = true.) I thought I could do this by using an XmlTextWriter with Formatting set to Formatting.Indented, but this doesn't work. The Formatting.Indented on the XmlTextWriter only indents my output when I...
2
7600
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 StreamWriter(m_optionsXmlFilePath, false); XmlSerializer xmlSerializer = new XmlSerializer(typeof(Options)); XmlTextWriter xmlTextWriter = new XmlTextWriter(sw);
1
5918
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 technical documents. Thanks, Terrence
4
5884
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 = @"C:\elements.xml"; XmlTextWriter xml = new XmlTextWriter(path, System.Text.Encoding.UTF8);
4
3115
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 XmlTextWriter(xmlFileName, null); xmlWriter.Namespaces = false;
4
8429
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
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9522
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10167
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9046
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7544
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5566
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.