473,320 Members | 1,858 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,320 software developers and data experts.

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(stm, System.Text.Encoding.UTF8)
writer.WriteStartDocument()
writer.WriteComment("This Is A List of My Books")
writer.WriteStartElement("MyBooks")
writer.WriteStartElement("Book")
writer.WriteAttributeString("ISBN", "1861005652")
writer.WriteAttributeString("Title", "Professional Visual Basic Interoperability")
writer.WriteElementString("Author", "Billy Hollis")
writer.WriteElementString("Author", "Rockford Lhotka")
writer.WriteEndElement()
writer.WriteEndElement()
writer.WriteEndDocument()

XmlTextReader xr = new XmlTextReader(stm)
XmlDocument xmldoc = new XmlDocument()
xmldoc.Load(xr)

But get...

The root element is missing
Nov 12 '05 #1
3 8279
Have you tried calling Flush or closing the writer after finishing writing
the data ?
I.e. after
writer.WriteEndDocument();
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*******@discussions.microsoft.com> wrote in message
news:43**********************************@microsof t.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(stm, System.Text.Encoding.UTF8);
writer.WriteStartDocument();
writer.WriteComment("This Is A List of My Books");
writer.WriteStartElement("MyBooks");
writer.WriteStartElement("Book");
writer.WriteAttributeString("ISBN", "1861005652");
writer.WriteAttributeString("Title", "Professional Visual Basic Interoperability"); writer.WriteElementString("Author", "Billy Hollis");
writer.WriteElementString("Author", "Rockford Lhotka");
writer.WriteEndElement();
writer.WriteEndElement() ;
writer.WriteEndDocument();

XmlTextReader xr = new XmlTextReader(stm);
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(xr);

But get....

The root element is missing

Nov 12 '05 #2
"Dave" <an*******@discussions.microsoft.com> wrote in message news:43**********************************@microsof t.com...
writer.WriteElementString("Author", "Rockford Lhotka");
writer.WriteEndElement();
writer.WriteEndElement() ;
writer.WriteEndDocument(); : :

writer.Flush( );
stm.Seek( 0, SeekOrigin.Begin);

: : XmlTextReader xr = new XmlTextReader(stm);
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*******@discussions.microsoft.com> wrote in message
news:43**********************************@microsof t.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(stm, System.Text.Encoding.UTF8);
writer.WriteStartDocument();
writer.WriteComment("This Is A List of My Books");
writer.WriteStartElement("MyBooks");
writer.WriteStartElement("Book");
writer.WriteAttributeString("ISBN", "1861005652");
writer.WriteAttributeString("Title", "Professional Visual Basic Interoperability"); writer.WriteElementString("Author", "Billy Hollis");
writer.WriteElementString("Author", "Rockford Lhotka");
writer.WriteEndElement();
writer.WriteEndElement() ;
writer.WriteEndDocument();

XmlTextReader xr = new XmlTextReader(stm);
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
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
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...
7
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...
1
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...
4
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...
1
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!! ****************************************...
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...
1
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...
4
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"/>...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.