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
0
anonymous (74722)
3/1/2004 8:16:08 PM
dotnet.xml 7266 articles. 0 followers. Follow

3 Replies
494 Views

Similar Articles

[PageSpeed] 12

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" <anonymous@discussions.microsoft.com> wrote in message
news:43CE3CBA-70F6-4A63-81CB-33941C67CB86@microsoft.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


0
Daniel
3/2/2004 3:20:33 AM
"Dave" <anonymous@discussions.microsoft.com> wrote in message news:43CE3CBA-70F6-4A63-81CB-33941C67CB86@microsoft.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


0
loresayer (274)
3/2/2004 3:46:43 AM
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" <anonymous@discussions.microsoft.com> wrote in message
news:43CE3CBA-70F6-4A63-81CB-33941C67CB86@microsoft.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


0
jiho.han (47)
3/2/2004 2:24:11 PM
Reply:

Similar Artilces:

How to create a CustomerID field for custom entities
Hi, The customerid field is used in opportunity and case and it allows you to select an account or contact, Great. I want to create this field in a form for a new entity i have made, but i cant work out how. Please help. Hi Michael, Customer is special composite type which is not available for custom entities in CRM 3. You will need to add two separete lookup fields for account and contact. -- uMar Khan :: MS CRM MVP CRM Freelance Consultant Email :: imumar at gmail dot com Blog :: http://umarkhan.wordpress.com MVP :: https://mvp.support.microsoft.com/default.aspx/profile/umar.khan &...

Creating a group of cells. Need Help Please.
Havn't used excel in a while and I need to create a group of cell corresponding to an input of a min and a max. Here are the details. On one sheet I have a box where you enter th min and a box where you enter the max. In another sheet I want column starting at A2 to output (MIN,A2+1000,A3+1000,....MAX) ho would I do this -- Thundersix ----------------------------------------------------------------------- Thundersixx's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=3055 View this thread: http://www.excelforum.com/showthread.php?threadid=50207 Name the...

Combining cells to create a formula
Hi, I have two cells that I want to combine to have a working formula B1 = sum B2 = d3 b3 = =b1&"("&b2&")" D3 = 10 The result in b3 is : sum(d3) How do I get this to result in the actual value in D3. I know I can simply write =D3, but the actual reason for combining the two cells is more complicated than explained here. Thanks Hi I think you are perhaps looking for the Indirect function =INDIRECT("D"&ROW(3:3)) would return the value contained in D3 As you copy down, the formula would alter to 4, 5 etc. represent D4, D5 etc. -- Regards R...

create a mass of mailboxes
hi and thanks in advance for your help, I've an exchange 2003 and I have to create a great number (3000) of account and mailboxes. I want to use a script to create them starting from a xls or csv file. There are same script that create the account in a progressive way but I can't use them. Is there something different to use? lucadb Try this link and refer to the section "Active Directory tools and application programming interfaces" http://www.microsoft.com/technet/prodtechnol/exchange/guides/e2k3InterOpMig/39d728f1-62e1-44aa-93c8-930fed46f4e1.mspx "lucadb"...

Creating a Microsoft Words document from an existing Excel spreads
Hi all, I need your kind guidance on how to create a new microsoft words document using an existing excel spreadsheet? At present, I have an excel spreadsheet, say "data.xls" containing data for example 6 fields such as COMPANY NAME, DATE, ITEM1, ITEM2, ITEM,3 & QUOTATION TOTAL. Under these 6 fields are some records of my customers. COMPANY NM DATE ITEM1 ITEM2 ITEM3 QUOTATION TTL ---------------- ------- -------- ------- -------- ------------------- ABC Limited 3 Apr xxxxx xxxxx ...

How do I send a welcome message to a new created mailbox automatically?
My exchnage send a message automatically but I need to change the message where do I change it? Thanks, Ron On Tue, 21 Jun 2005 16:35:53 -0700, "Ron" <rony@geotestinc.com> wrote: >My exchnage send a message automatically but I need to change the message >where do I change it? > >Thanks, >Ron > Exchange doesnt do that unless you have something scripted. If you are referring to the welcome message in Outlook, its called welcome.msg , but I dont think its been used since Outlook 2002. So where would I plug in the script? Also I searched for welcome.ms...

Creating a template using Publisher
I need help creating a neighborhood directory template using Publisher and merging an Excel spreadsheet into it. It keeps printing multiple pages of the first page after I merge. What version Publisher? If you have 2003 use the catalog merge for your directory. http://office.microsoft.com/en-us/assistance/CH010504381033.aspx Otherwise you need to setup your page as though it is labels. Determine the size of the area you need for each entry, select labels in page setup, type the size you determined in the page setup. Adjust the gaps and margins when you are ready to print (2000 and bel...

Creating a "Do_Not_Reply" e-mail address
I need to create a do-not-reply@domain.com address that any user that I give permission to on the domain can send as by putting the above address in the "From:" field of their Outlook client and the external recipient will receive this and only see that it came from the do-not-reply address and not the send on behalf of user. Also what would be the best way to block or not receive the e-mails even if an external user does reply to it? On Fri, 13 Apr 2007 20:56:22 -0700, "D-a-n_L" <dlajoie@hotmail.com> wrote: >I need to create a do-not-reply@domain.com addr...

Linked graphics into a Publisher 2000 document
I have a 400KB document (withgout graphics). I insert two JPEG images each around 200KB into the document. I figure the resulting document should be around 700KB. Result? An 11MB file. I delete the images (document is now 400KB) and instead insert the images using the "Link to File" option. Graphics Manager confirms that the images are linbked into the file. The size should not be more than 500KB. Result? An 11MB file. Question is how can I insert images into a Publisher 2000 documentr without the file blowing up? And why does it blow up? Regards, Richard. Richard wrot...

How to get XML data out of an XML file
I am trying to retrieve the Parameters first or second (0, 1 ,2) node from the following XML file: <?xml version="1.0" encoding="utf-8" ?> <Robot xmlns="http://tempuri.org/RobotDefaults.xsd"> <Parameters> <Name>Decker</Name> </Parameters> <Parameters> <Name>A</Name> </Parameters> <Parameters> <Name>B</Name> </Parameters> </Robot> There will be more data than just a name for each Parameters node. Here is my code: Dim node As XmlNode = xmlDocument.SelectSingleNode("/P...

Can I create sub-functions in a cross-functional flowchart?
Trying to create a template for my organization to map processes to. Goal is to map the organization to the 'function bars' in Visio's cross-function flowcharts. However, I would like to create some 'sub-functions' for lower organizational elements (i.e. divisions under a department). Is this possible?? ...

Any XML Naming Conventions?
Does the use of DTD, XML Schema and similar constructs adopt the use of C# naming conventions? If so how do I make the distinction of how to apply C# conventions with XML elements, attributes and so on? Any referrals to resources that discuss or document XML Naming Conventions? -- <%= Clinton Gallagher, "Twice the Results -- Half the Cost" Architectural & e-Business Consulting -- Software Development NET csgallagher@REMOVETHISTEXTmetromilwaukee.com URL http://www.metromilwaukee.com/clintongallagher/ clintonG wrote: > Does the use of DTD, X...

XML as a database that can be written to
Has anyone had any luck using XML as a database that not only can be read, but also written to? What features are available in .NET to do this? I need to do all of this from a Web based application. Am I thinking File System Object? Thanks. mjc wrote: > Has anyone had any luck using XML as a database that not only can be read, > but also written to? That's doable, but you better use real database instead. XML wasn't meant to be used as a database, it's just a data format. -- Oleg Tkachenko [XML MVP] http://blog.tkachenko.com Yes, it can be done and I think its an id...

Problems with loading xml from XmlTextWriter
I use an XmlTextWriter to generate xml data. The declaration is: System.Text.StringBuilder sb = new System.Text.StringBuilder(); System.IO.StringWriter sw = new System.IO.StringWriter(sb); XmlTextWriter w = new XmlTextWriter(sw); And I write to it using .... w.WriteElementString("foo","bar"); When the creation is done, I need to load it into DOM to select an XmlNode or an XmlElement to apply to another method (which I cannot control). I thought I could do it with: XmlDocument xDoc = new XmlDocument(); xDoc.Load(sb.ToString()); XmlNode node = xDoc.SelectSingleNode(&...

New Named Range Created Each Time Data Imported into Excel via Macro
I have noticed that each time I import data into an Excel spreadsheet via a macro, a new named range (for the same range) is created. This does not pose a problem, but after a while, I'll have a huge number of named ranges that will never be used. Why does Excel name the range and how can I stop this? I noticed in the recorded macro, there was a line .Name = "drd_5". I commented this out to see what would happen, but it just renamed the range "ExternalData_5". The next one was "ExternalData_6", etc. Thanks, Carroll Rinehart You could refresh with differen...

Creating a worksheet in Spanish
Hello, Is there a way to create a worksheet on MS XP Pro running Office 2003 Pro all in Spanish? Thank You Kent Hansen Hi Kent All valid formulas -except the ones from the Analysis Toolpack- will automatically translate to the language of the Excel it's opened in. So any spanish speaking Excel will translate formulas to spanish, no matter which version it was written in. The rest, text constants and such, has to be typed or pasted. Excel will not translate this herself. HTH. Best wishes Harald "Kent" <Kent@discussions.microsoft.com> skrev i melding news:ACCCBA3A-53B4...

Move pages from document to document?
This is a multi-part message in MIME format. ------=_NextPart_000_0397_01C7D5D9.22350CB0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi, I am relatively new to this software. I can't seem to find how to move a = page from one document to another. Any help appreciated. Shmuel ------=_NextPart_000_0397_01C7D5D9.22350CB0 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD> <MET...

5 steps to create a publication,the workplan
I need to know at least 5 steps to creating a workplan in pulisher. homework? 1- turn on computer 2- open Publisher 3- File | New 4- File | Page Setup 5- Design your thing 6- File | Save your thing oops thats 6 "thinking" <thinking@discussions.microsoft.com> wrote in message news:1B4E2A10-BCB0-473D-B52A-EFCBB658D856@microsoft.com... |I need to know at least 5 steps to creating a workplan in pulisher. Thinking? This newsgroup is dedicated to solving problems with MS Office Publisher. Not doing your class assignment. "thinking" <thinking@discussions.micros...

How do I break a large word document into smaller components?
I have a very large Word document (over 40 MB) that I update several times a year. I would like to break it into smaller components so that only the affected component would have to be backed up online. However, I still wish to maintain the ability to create a table of contents and index for the whole document. Is this possible and if so, what is the process to do it. I am using MS Office 2010 beta running MS Windows 7 Home Premium on a 64 bit Dell desktop. Hi RoadRoy, You can simply make multiple copies and delete whatever you don't want from each copy. For the mul...

Unable to add documents to My Recent Documents
Hello, Any suggestions as to how to fix "My Recent Documents"? No new documents add to the folder and it remains persistently empty. I get an "Access denied" message when attempting to go to customizing the Start menu advanced tab. Are you Right-Clicking on the Start Button and then selecting Properties from the list? If you select Properties then You need to select Customize from Start Menu Tab. Then from Advanced Tab you should ensure there is a TICK next to "List my Most Recently Opened Documents". hth Gary V wrote: > > Hello, &g...

comparing numerically two documents
Hi all, I need to compare the text of document A with the text of document B with some Word functionality, plugin or tool that tells me how much the text in the two docs is different. Some kind of reporting feature that tells you something like "The text of document B is X % (percent) different from the text of document A" Is anythig like this available? thanks ...

Creating PDF files in MFC
Hi, I want to create PDF files in MFC. Does anyone know how should I do it? Thanks in advance. Check out the following http://sourceforge.net/projects/pdfcreator/ "Shahoo" wrote: > Hi, > I want to create PDF files in MFC. Does anyone know how should I do > it? > Thanks in advance. > > Here's another to try out: "Shahoo" <shahookamangar@gmail.com> wrote in message news:1175272390.190193.274530@n59g2000hsh.googlegroups.com... > Hi, > I want to create PDF files in MFC. Does anyone know how should I do > it? > Thanks in advanc...

LOOK AT- x-PUBS LONDON- XML.....
Hi Have you seen www.x-pubs.com I think it will be very interesting for your audience. X-Pubs is Europes largest XML publishing conference. We have Ann Rockley, JoAnn Hackos, UBM, Mark Logic, XMetal, Idiom all speaking plus a lot more ...

Mail Merge with Word 2003
Please Help if you can. I have lost hours editing a Mail Merge Master document, only to find none of the changes are saved when you reopen. Here's what happened: I want to edit the fields in a mail merge document. I want to search for the {OLD-FIELD} and replace it with the {NEW FIELD}. Word does not seem to allow me to do this. First, it will not let me cut and paste the fields into the Search & Replace Box. Secondlly, after hours of laboriously replacing my Fields one at a time, and dutifully saving, NONE of the changes were saved. The changes are all shown in ...

Can I create a CScrollView popup window?
Hi, My application displays video from a firewire camera. The video is displayed in a CScrollView derived window that I create in CMainFrame::OnCreate() like this: m_pImageView = new CImageView; m_pImageView->Create(NULL, NULL, WS_CHILD | WS_VISIBLE, rClient, this, AFX_IDW_PANE_FIRST, NULL)) Now, when the user stops the video, they can click a toolbar button that pops up a CWnd derived window that displays all of the image data on a particular line of video. So, if the camera is spewing out 8-bits per pixel in the 800x600 mode, the popup window would display the 800 bytes associated w...