469,286 Members | 2,476 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,286 developers. It's quick & easy.

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 8147
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by androger | last post: by
7 posts views Thread by pintihar | last post: by
1 post views Thread by Mike P | last post: by
4 posts views Thread by Brad Wood | last post: by
1 post views Thread by Point.Cube | last post: by
4 posts views Thread by John Salerno | last post: by
1 post views Thread by jschell | last post: by
4 posts views Thread by FabrizioSW | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.