473,657 Members | 2,932 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 8301
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
1728
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
9710
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
5202
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
4304
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
1408
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
1133
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
5863
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
3009
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
4752
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
8384
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
8601
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
7314
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
6162
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
5630
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
4150
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
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.