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
462 Views

Similar Articles

[PageSpeed] 15

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:

System.xml.xmlnode class and XSD file
Hello. I'm trying to develop a web comsuming client using VS.NET 2003(VB), .NET Framework 1.1.4322, ASP.NET 1.1.4322, WSE2.0 on a WinXP Pro Sp2 comuter. I've done other web consuming client programs for services that passed datatables. This service i am trying to consume has it's function type as system.xml.xmlnode. When i added the web service, i thought it would generate an XSD for me, but it did not. It did for previous programs. I'm not sure if is supposed to. The service i am trying to consume is at: https://xatanet.net/xatanetwebservice/tripexportservice.asmx...

Cannot create new PO
We cannot create new PO in the system. We are getting an error "Item Engineering for this Item must be configured" when we try to create a new PO and select a line item Any ideas? I cannot think of anything significant, but this started after we configured Item Resource Planning for Items for use with PO Generator application. Hi Aman, I had the same issue when MFG was installed. It appears that the Item Engineering Data must exist for all items you want to use. Instead of entering Item Engineering Data for all items, I ran an SQL query to populate the Item Engineering Data t...

Multiple reports in same Document
I am looking for a way to show on the same document, two different type of data as follows: I have a data table with any number of columns that are set in width. I am wanting to insert a pivot table report imediately below the dat table. The pivot table picks up the column width from the data tabl already in the sheet. This causes the pivot table fields to be eithe too large or too small. If I correct the pivot table, then the dat table attributes are changed. Thanks -- JGallik0 ----------------------------------------------------------------------- JGallik01's Profile: http://www...

Creating Universal Groups with Distribution Group members?
Hello all. We have a Windows 2003 Active Directory with Exchange 2003, and I don't really want to duplicate work, but I think I may have to (ugh!). Here is the deal. I need to create an All Managers Universal Group with every group that has managers in it. So, what I tried doing, was I created a Universal Group and added the _All Managers DL into it -- but that didn't seem to work, according to my boss who asked me to create this UG for him. So, my question is, am I not allowed to add DL members to a Universal Security Group? And if now, is there any way that I can take...

when is the time right to create a sub to do something?
hello i have a project where i have lots of fields that filter things on the forms. i have the typical apply filter button, but i have also been using a key catch for the enter key. when a user updates the filtering field and hits enter i cause it to requery the sub query that the filtering field creates a filter for. on some forms the amount of times i put in the code: If KeyAscii = vbKeyReturn Then Me.mySubform.Requery End If is ridiculous. so the question is should i make a routine for the form(s) that calls the little bit of code? it seems so repeatative to put th...

Document Imaging
I just received a e-mail on Office 2003. In there they had a article on Publisher and Document Imaging print driver. I like it but how do you save multiple images in tiff format in one file? Would you not have thought it smart to go to the Office newsgroup! It pops up a dialogue box if you want more pages. You can import pages into an existing file. It is a great little app, just wish MS would fix the bugs. It really is a great pity MS is such a lousy disgusting company when it comes to quality control. -- "If you don't know where you are going, any road will take you there!&qu...

Creating Bubble Charts
How do you create a bubble chart that displays the legend as a description of the 3rd variable (the bubble size)? For example, how would you create the following bubble chart where the x-axis is column B, y-axis is column C, bubble size is column D and legend is column A? A1: B1:Gross Rev C1:Net Income D1:# of Plants A2:East B2:830,000 C2:35,000 D2:26 A3:West B3:620,000 C3:54,000 D3:13 A4:North B4:150,000 C4:80,000 D4:40 A5:South B5:41,000 C5:15,000 D5:35 Any insight will be helpful! Hi Tina I would create a bubble chart and use one of the following tools for labelling the...

Create a button to find the next record
Hi I am trying to create a button to find the next record for a particular studentID. There is a main form and subform. I want to be able to find all relevent subforms for a particular student. The master link id is STUDENTID and the child link id is STUDENTID_FK. So for student #1, when I click on the subform and click on the button, it would cycle through all this student's records. What is the best way to set up this button? Thank you for your help in advance. You already have one that is built in. Just click the navigation button right of the record number ...

i created folders and now they are gone help!
I'm on outlook 2007, put important emails in there and poof, gone! it's magic Check your View. "jdoubleh" <jdoubleh@discussions.microsoft.com> wrote in message news:3EEAA52B-897C-437D-AA01-15983153EBAC@microsoft.com... > I'm on outlook 2007, put important emails in there and poof, gone! ...

How/where to store my encoded HTML? In DB? In XML?
We have a parent-child table set up to maintain content. When this table is updated, I do a recursive call through the data and spit out an XML file. Then, when we want to display this content, I load up the XML file, attach the XSL file to it, and do the transformation via VB.net and send it off to the browser. The problem is that much of this content is HTML. As such, I'm storing it in the database as encoded HTML. I'm confused as to when/where I should decode it. For starters, I just grabbed the raw data from the DB, spit out the XML, and then in my XSL file, added a few ...

Creating new data file
How do I create a new data file in Outlook. at the moment I have one personal folder that contains every contact and mail in sub folders. I wish to create a business file that is solely for that purpose and is seperate from any of my other contacts. E.G. Business Folder> Enquiries Sales Purchases Orders Pending Shipped Contacts My server TN4 my Client win98 1. How do I create postoffice in server ? (with outlook 2000) 2. how do i create out look 2000 in my client win98? please send me about detail procedur conecting installation in server and client. thank four your attentions ...

Error creating Distribution group SBS 2003/Exchange 2003
When I try and create a mail enabled dist group I get an error message that "Exchange could not create public folder" Then if I choose OK it says "Do you want to keep this dist group?" I have tried everything I can think of. We have to create these folders ASAP. How can I troubleshoot this? I tried network monitoring but that didn't help - unless I am doing something wrong. SBS and Exchange on same server obviously. Dan "Danno" <Danno@discussions.microsoft.com> wrote: >When I try and create a mail enabled dist group I get an error message tha...

Creating a form from two tables
Im looking for assistance. This database is used for evaluations. so a manager completes a form that has 25 questions. They do 4 evaluations a month on each employee. I have 3 tables the Evaluation information (Table 1), The Answer Information (Table 2) and the Question Table (Table 3) Is there a way to popuplate at form that has all the Evaluation Information, lists each question from table 3 with the Answer Information for each of those questions from Table 2. I haven't begun creating this database yet so if there is a better way to do it i am open to suggestions. Tabl...

Xml Serialization 01-19-10
Hello, I have this class(Someone elses code): public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable .................... The class creates an object that resembles a Generic Dictionary and now I want to convert it to one (Generic Dictionary) Do I have to loop through the keys and values and add them to mine? Or is there an easier way? Thanks * coder316 wrote, On 19-1-2010 18:14: > Hello, > I have this class(Someone elses code): > public class SerializableDictionary<TKey, TValue> > > : Dicti...

How do I back up my complete "my document" file?
I do not know how to back up my "my documents" file and would appreciate knowing how. Do you have any sort of large writeable media (writeable CDs, DVDs, etc)? If so you could try copying your My Documents folder on to them "Dougal" <Dougal@discussions.microsoft.com> wrote in message news:8DD55FCB-B576-408F-9BFC-B736ABE17CE6@microsoft.com... >I do not know how to back up my "my documents" file and would appreciate > knowing how. The next issue of "Computeractive" will cover this in some depth. "Dougal" <Dougal@discussi...

Is there a way to create a customer log in section,
Is there a way to create a customer log in area, so that I can create log in names and passwords? You will probably need to use php and a MySQL db to do this. Mike Ottolikestogetbloto wrote: > Is there a way to create a customer log in area, so that I can create log in > names and passwords? ...

showing document data
I am doing an experiment and am wondering if someone would tell me why the following OnDraw insertion does not display text. if(m_bFirstCall) if(pDoc->OnOpenDocument(_T("C:\\Testfile.txt") ) ) { m_bFirstCall = false; ::Beep(500,50); } UpdateWindow(); How is that going to display text? You will have to use the DrawText/TextOut(...) calls on the device context to display the text on a view. -- Vipin Aravind http://www.explorewindows.com/Blogs "Steve Russell" <srussell@removethisinnernet.net> wrote in message news:OAs7j0B1GHA.4116@T...

ACCESS 2007, want to create comment/remarks section.
To any MS ACCESS 2007 users: I'm using a Contact List Database and there should be a plus sign to the left of each record that acts as a toggle to open and close a comments section for typing the results of each phone call I make. Does anyone know how to add this feature to an existing database? On Wed, 27 Jan 2010 11:07:02 -0800, MusicFan381 <MusicFan381@discussions.microsoft.com> wrote: >To any MS ACCESS 2007 users: I'm using a Contact List Database and there >should be a plus sign to the left of each record that acts as a toggle to >open and close a co...

SMTP address created by RUS
This is not a strict Exchange problem but I have run out of ideas. I have exchange 2003 and I want use Recipient Policies to create SMTP addresses for my users, there are six different domain names I need to use (.co.uk .com .de etc) The only way I can see to do this is by populating a user attribute and filtering against it in the recipient policy (at the moment I am using 'City' is exactly .......) but because this is set sometimes after the RUS has run the only way to correctly populate the proxyAddress is by re-applying the policy. I want a way of filtering against something...

XML Application
We use xml files to denote file configurations for our company. We are now getting to the point where need a browser based method of maintaining and creating the configuration files. I had written a small test app for working with XML in ASP.Net and have found that it is painfully slow and cumbersome. Can anyone give me some suggestions or sample links to articles that would help develop an application of this type? I need to allow the user to create a new configuration xml document and modify them with the usual updates, inserts and deletes of nodes. Thanks, Matt ...

How do I create new default color settings for charts in Excel?
I do not like the default color settings for my charts in Excel. I always end up modifying them extensively. I'd like to make as few changes as possible when my charts are created. Can I set new color defaults for charts created in Excel? Hi, You can set the colours for chart lines and fill. Menu Tools > Options. On the Color tab is a set of colours for Line and Fill. Cheers Andy pro user wrote: > I do not like the default color settings for my charts in Excel. I always end > up modifying them extensively. I'd like to make as few changes as possible > when my char...

How to create a saved view within CRM for postcode ranges
Is there anyway of generating a saved view of employer postcodes between certain ranges (eg, Between 4000 and 4800) within CRM? ...

How do i create a record form out of each rows?
Version: 2008 Operating System: Mac OS X 10.6 (Snow Leopard) Processor: Intel Hi <br><br>I would like to transform rows into a record form. <br><br>Instead of info showing in rows <br><br>I want it to look like a description. <br><br>Example <br><br>Artist: Michael Jackson <br> Album: Thriller <br> Label: Epic <br> Code: EC-4782 <br><br>Thanks <br><br>Dom Hi Dom, it largely depends on what you want to do with the form. I would recommend using Word and its Mail Merge Manager to c...

Sort XML Into Hierarchy
I am generating an xml file off an employees table from a database to use to databind a treeview to on a master page rather than call the db every time the page loads. I need to take this XML and sort it hierarchially so when the treeview gets it, it shows all employees sorted with child nodes for employees who report to them, etc. I have never worked with XML but found enough online to assume an xsl file would do the trick, provided it was written correctly. What I have so far, thats not working, is this. CODE TO GENERATE XML FILE FROM DB DataSet SQLDataset = new DataSet("Employe...

excel 97 xml import
I have Excel 97 and need to import a XML file. I don't know vba. Slinger XL97 was written way before XML therefore it will not handle it. Only XL2003 (Individual product or from Office Pro) will recognise and handle XML import/export -- HTH Nick Hodge Microsoft MVP - Excel Southampton, England nick_hodgeTAKETHISOUT@zen.co.uk.ANDTHIS "Slinger" <Slinger@discussions.microsoft.com> wrote in message news:D1D9952E-F741-4B11-8040-B27B34F382BE@microsoft.com... >I have Excel 97 and need to import a XML file. I don't know vba. ...