473,325 Members | 2,671 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,325 software developers and data experts.

Load/save XML from/to stream.

Hi, all!

What is wrong in the follow code:

MemoryStream streamSave = new MemoryStream();
XmlDocument doc = new XmlDocument();
doc.LoadXml("<root></root>");
doc.Save(streamSave);

MemoryStream streamLoad = new MemoryStream();
XmlDocument doc2 = new XmlDocument();
byte [] byteArr = streamSave.ToArray();
streamLoad.Write(byteArr, 0, byteArr.Length);

doc2.Load(streamLoad); - i get the error "The root element is missing"

Thanks in advance,
Evgeny
Nov 12 '05 #1
2 17147
Evgeny wrote:
What is wrong in the follow code:

MemoryStream streamSave = new MemoryStream();
XmlDocument doc = new XmlDocument();
doc.LoadXml("<root></root>");
doc.Save(streamSave);

MemoryStream streamLoad = new MemoryStream();
XmlDocument doc2 = new XmlDocument();
byte [] byteArr = streamSave.ToArray();
streamLoad.Write(byteArr, 0, byteArr.Length);
Usual mistake - you forgot to rewind the stream here:

streamLoad.Position = 0;
doc2.Load(streamLoad); - i get the error "The root element is missing"


--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog
Nov 12 '05 #2
Thanks a lot!

"Oleg Tkachenko" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:u9**************@TK2MSFTNGP10.phx.gbl...
Evgeny wrote:
What is wrong in the follow code:

MemoryStream streamSave = new MemoryStream();
XmlDocument doc = new XmlDocument();
doc.LoadXml("<root></root>");
doc.Save(streamSave);

MemoryStream streamLoad = new MemoryStream();
XmlDocument doc2 = new XmlDocument();
byte [] byteArr = streamSave.ToArray();
streamLoad.Write(byteArr, 0, byteArr.Length);


Usual mistake - you forgot to rewind the stream here:

streamLoad.Position = 0;
doc2.Load(streamLoad); - i get the error "The root element is missing"


--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog

Nov 12 '05 #3

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

Similar topics

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...
2
by: gnv | last post by:
Hi all, I am writing a cross-browser(i.e. 6 and netscape 7.1) javascript program to save an XML file to local file system. I have an xml string like below: var xmlStr = "<?xml version="1.0"...
3
by: user | last post by:
Hello I finally managed to save Bitmap that reprezented my pictureBox to jpg file, but now how can i load from that file to Bitmap again ? Bitmap does not have any Load or Open method (like...
2
by: Olaf Baeyens | last post by:
I want to convert one bitmap file to another one. For example load as bmp and save as jpg. The loading part is simple I do this: Stream BitmapStream =...
7
by: Mac | last post by:
Hi all I have a asp.net application which calls a web service via a wdsl file which retrieves an XML String. The XML String is from a adodb recordset saved as via a stream. Dim oRS as New...
2
by: Dave | last post by:
Hello. I need to load an URL and save it to a file in Asp.Net. The function below is creating the file, but isn't putting the data in it. Also the data is binary, so I'm not sure if I need to...
4
by: jmhmaine | last post by:
The following code works when Option Strict is off: Dim thumbSize As New Size thumbSize = CType(NewthumbSize(g.Width, g.Height, 100), System.Drawing.Size) Dim imgThumbOutput As New Bitmap(g,...
2
by: john0600 | last post by:
I'm writing a web service in .NET to work with Delphi clients. Delphi uses ADO so I want to convert the Dataset to a Recordset and vice- versa. It appears a Web Service cannot publish a web method...
1
by: not_a_commie | last post by:
Here are a few thoughts for loading and storing your xml using the XmlDocument class: First, don't ever insert the header manually. This is to be done by the writer. In other words, don't do...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.