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

XmlDocument Save and Load Streams

I have created an XmlDocument and can see that this is correctly formed and has a root element
If I Save the doc to a file and reload it all works o
If I dump the doc to a stream, again using the Save method, and write this to a file I can then reload this correctly.

However since I have no need to write the data to file I want to save it as a stream then reload it the stream.
However If I do this I have suddenly lost the root element

<<I am actually going to create an XmlTextReader from the stream but the above demonstrates my problem >><<(which occurs also when trying to read an XmlTextReader created from the stream) >

Can anyone explain what I am doing wrong

CODE
THIS FAIL
XmlDocument xmlDoc = GetXmlDoc( ); // Returns an XmlDocument
MemoryStream xmlStream = new MemoryStream( )
xmlDoc.Save( xmlStream )
xmlDoc.Load( xmlStream ); // Exception thrown here - root element is missin

THIS WORK
XmlDocument xmlDoc = GetXmlDoc( ); // Returns an XmlDocument
MemoryStream xmlStream = new MemoryStream( )
xmlDoc.Save( "C:\\TEMP\\TEST.xml" )
xmlDoc.Load( "C:\\TEMP\\TEST.xml" )

Jul 21 '05 #1
2 22392
Tom Pearson <to*********@ds-s.com> wrote:
I have created an XmlDocument and can see that this is correctly formed
and has a root element.
If I Save the doc to a file and reload it all works ok
If I dump the doc to a stream, again using the Save method, and write
this to a file I can then reload this correctly.

However since I have no need to write the data to file I want to save
it as a stream then reload it the stream.
However If I do this I have suddenly lost the root element.

<<I am actually going to create an XmlTextReader from the stream but
the above demonstrates my problem >><<(which occurs also when trying
to read an XmlTextReader created from the stream) >>

Can anyone explain what I am doing wrong?

CODE:
THIS FAILS
XmlDocument xmlDoc = GetXmlDoc( ); // Returns an XmlDocument
MemoryStream xmlStream = new MemoryStream( );
xmlDoc.Save( xmlStream );
xmlDoc.Load( xmlStream ); // Exception thrown here - root element is missing


Yup - you're trying to load from the *end* of the stream. If you use:

xmlDoc.Save(xmlStream);
xmlStream.Flush();
xmlStream.Position=0;
xmlDoc.Load(xmlStream);

it should work fine.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
Cheers for that

I don't half feel stupid now though!
Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: KathyB | last post by:
If someone could just explain this to me...I just don't get it! I have an aspx page where I retrieve several session variables and use xmlDocument to transform xml file with xsl file into an...
5
by: Jack Fox | last post by:
I'm still new to Xml, but finding it very useful. Conceptually what I want to do is compress an XmlDocument, save it to SQL Server as image data, read it back and restore it to an XmlDocument. ...
1
by: Shawn | last post by:
Hi. I'm using a FileStream (instead of just the path to the xml file) to load an XmlDocument. I'm doing this because I need to be able to prevent other processes to update the file I'm working on....
1
by: Fahad Ashfaque | last post by:
Hello, I am finding trouble while using XmlDocument class to parse my xml documents reside in file system. The problem is, I have a function that loads the XmlDocument and the function is...
0
by: Pierre | last post by:
Hi, I'm trying to select specific nodes from a XmlDocument filled with a serialized object and to insert these nodes into another XmlDocument. The object is well serialized (see below). From a...
2
by: Tom Pearson | last post by:
I have created an XmlDocument and can see that this is correctly formed and has a root element If I Save the doc to a file and reload it all works o If I dump the doc to a stream, again using the...
1
by: Gustaf | last post by:
I have a class that contains a certain kind of XML document. The input file is *not* an XmlDocument, but when loaded, I want it to work like the XmlDocument class, like: MyDocument doc = new...
1
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
The description of the XMLDocument.Load method doesn't quite answer the question. When passing in a FileStream object to the Load method, does it load the entire document into memory? For...
2
by: yxq | last post by:
I want to add some elements to a XML file using XmlDocument, how to do? thank you very much. The original XML file like: //////////////////////////////////////////////////////// <?xml...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.