473,725 Members | 1,980 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to create an XML document with XmlTextWriter?

Hi, I'm trying to create an XML document with XMLTextWriter but I want to store it in memory (not write it to a file like so many examples do). I tried the following but with no luck. Should I not user this object for this purpose? Thanks, Dave

MemoryStream stm = new MemoryStream()
XmlTextWriter writer = new XmlTextWriter(s tm, System.Text.Enc oding.UTF8)
writer.WriteSta rtDocument()
writer.WriteCom ment("This Is A List of My Books")
writer.WriteSta rtElement("MyBo oks")
writer.WriteSta rtElement("Book ")
writer.WriteAtt ributeString("I SBN", "1861005652 ")
writer.WriteAtt ributeString("T itle", "Profession al Visual Basic Interoperabilit y")
writer.WriteEle mentString("Aut hor", "Billy Hollis")
writer.WriteEle mentString("Aut hor", "Rockford Lhotka")
writer.WriteEnd Element()
writer.WriteEnd Element()
writer.WriteEnd Document()

XmlTextReader xr = new XmlTextReader(s tm)
XmlDocument xmldoc = new XmlDocument()
xmldoc.Load(xr)

But get...

The root element is missing
Nov 12 '05 #1
3 8312
Have you tried calling Flush or closing the writer after finishing writing
the data ?
I.e. after
writer.WriteEnd Document();
add this line:
writer.Close(); // alternatively, you can call writer.Flush()

--
Daniel D.C. [MSFT]
This posting is provided "AS IS" with no warranties, and confers no rights
"Dave" <an*******@disc ussions.microso ft.com> wrote in message
news:43******** *************** ***********@mic rosoft.com...
Hi, I'm trying to create an XML document with XMLTextWriter but I want to store it in memory (not write it to a file like so many examples do). I
tried the following but with no luck. Should I not user this object for
this purpose? Thanks, Dave.
MemoryStream stm = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(s tm, System.Text.Enc oding.UTF8);
writer.WriteSta rtDocument();
writer.WriteCom ment("This Is A List of My Books");
writer.WriteSta rtElement("MyBo oks");
writer.WriteSta rtElement("Book ");
writer.WriteAtt ributeString("I SBN", "1861005652 ");
writer.WriteAtt ributeString("T itle", "Profession al Visual Basic Interoperabilit y"); writer.WriteEle mentString("Aut hor", "Billy Hollis");
writer.WriteEle mentString("Aut hor", "Rockford Lhotka");
writer.WriteEnd Element();
writer.WriteEnd Element() ;
writer.WriteEnd Document();

XmlTextReader xr = new XmlTextReader(s tm);
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(xr) ;

But get....

The root element is missing

Nov 12 '05 #2
"Dave" <an*******@disc ussions.microso ft.com> wrote in message news:43******** *************** ***********@mic rosoft.com...
writer.WriteEle mentString("Aut hor", "Rockford Lhotka");
writer.WriteEnd Element();
writer.WriteEnd Element() ;
writer.WriteEnd Document(); : :

writer.Flush( );
stm.Seek( 0, SeekOrigin.Begi n);

: : XmlTextReader xr = new XmlTextReader(s tm);
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(xr) ;


The Flush( ) is necessary for most buffered I/O Streams, so
that content written to the stream's buffer, but not yet above
the threshold for being committed, is committed.

Additionally, after writing XML to the Stream, the Stream "pointer"
is at the end of the stream, positioned after the last content written.
In order for the XmlTextReader to read from the beginning of the
Stream, that "pointer" must be re-positioned to the start of the
stream.
Derek Harmon
Nov 12 '05 #3
Most likely, all you need to do is set MemoryStream's position to 0 right
before feeding it to the reader. i.e. stm.Position = 0.
MemoryStream is very convenient and I use it quite often. I guess you want
to close your writer if you are done at that point though and it probably
wouldn't hurt.

Jiho

"Dave" <an*******@disc ussions.microso ft.com> wrote in message
news:43******** *************** ***********@mic rosoft.com...
Hi, I'm trying to create an XML document with XMLTextWriter but I want to store it in memory (not write it to a file like so many examples do). I
tried the following but with no luck. Should I not user this object for
this purpose? Thanks, Dave.
MemoryStream stm = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(s tm, System.Text.Enc oding.UTF8);
writer.WriteSta rtDocument();
writer.WriteCom ment("This Is A List of My Books");
writer.WriteSta rtElement("MyBo oks");
writer.WriteSta rtElement("Book ");
writer.WriteAtt ributeString("I SBN", "1861005652 ");
writer.WriteAtt ributeString("T itle", "Profession al Visual Basic Interoperabilit y"); writer.WriteEle mentString("Aut hor", "Billy Hollis");
writer.WriteEle mentString("Aut hor", "Rockford Lhotka");
writer.WriteEnd Element();
writer.WriteEnd Element() ;
writer.WriteEnd Document();

XmlTextReader xr = new XmlTextReader(s tm);
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(xr) ;

But get....

The root element is missing

Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1732
by: androger | last post by:
hi, I have a huge xmldocument that represent data from a table. How can I break this down into multiple xmldocuments, say of a certain number of records each?
0
9720
by: Riley Phelps | last post by:
When I use the XML Serializer in the following example, the first Serialize (with the class Pets) works, but the second (with the cplaa Pets2) fails. Which arrtubute nedds to be applied to make the second Serialize work namespace SerializeTes public class Class args Test _test = new Test() _test.TestAnimal()
7
5205
by: pintihar | last post by:
Hi, As a follow on from an earlier post I have another question about xslt. Is it possible to create the stylsheet programatically? Is this sensible? In the first phase I needed to map element name from inbound xml to my internal elements to standardize disparate input. Now I could just create an xslt stylesheet for each possible inbound format and be done, but I think it would be powerful to be able store this mapping in a database and...
1
4307
by: Mike P | last post by:
I am trying to create a new file and write to it, but I keep getting the error 'cannot access the file because it is being used by another process'. Can anybody help me out? long lngNextNumber = Find_Next_File_Number(); string strNewFile = "C:\\inetpub\\wwwroot\\webapplication90\\xml_files\\test" + lngNextNumber + ".xml"; File.Create(strNewFile);
4
1411
by: Brad Wood | last post by:
I need to build a document by adding sub docs to it. When I do so, I lose formatting. I've tried a suggestion I saw of simply setting the formatting a second time to no avail. Following is a console snippet that demonstrates the problem: static void Main( string args ) { StringWriter sw = new StringWriter(); XmlTextWriter xw = new XmlTextWriter( sw );
1
1138
by: Point.Cube | last post by:
Here is my code, but the problem is I want to recreate a new file but at the and I find that the program add the new element the old ones!! **************************************** System.IO.File.Delete( mXmlFile); System.Xml.XmlTextWriter XmlWtr = new System.Xml.XmlTextWriter( mXmlFile, null); XmlWtr.Formatting = System.Xml.Formatting.Indented; XmlWtr.WriteStartDocument();
4
5881
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);
1
3012
by: jschell | last post by:
The following two cases behave differently in Net 2.0. The Case_Create_StringWriter throws an exception while the Case_XmlTextWriter does not. Is there a problem with this code? Or is this a known problem? -------------------------------------------------------------------------- using System; using System.Xml;
4
4757
by: FabrizioSW | last post by:
Hi all i've to create a xml doc like this <?xml version="1.0" encoding="UTF-8"?> <Main xmlns:x="http://www.w3.org/1999/XML/xinclude"> <x:include href="one.xml"/> <x:include href="two.xml"/> <x:include href="more.xml"/> </Main> i tried to use xmlnamespacemanager and also to declare the namespace
0
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9174
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
8096
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
6702
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
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4517
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
4782
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2634
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.