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

DeflateStream & MemoryStream?

Hi,

I am trying to Compress & Decompress a DataSet.
When running this code I am receiving the error:

System.Xml.XmlException was unhandled
Message="Root element is missing."
Source="System.Xml"

The code:

DataSet dsDataOrginal = new DataSet();
DataSet dsDataAfterDecompress = new DataSet();
dsDataOrginal.ReadXml(@"C:\VSProjects2005\ZIP_Comp ress\TheXML.XML");

//Compressing to Memory
MemoryStream MS = new MemoryStream();
DeflateStream dfs = new DeflateStream(MS,
CompressionMode.Compress, false);
dsDataOrginal.WriteXml(ms, XmlWriteMode.WriteSchema);

//DeCompressing from Memory
MS.Position = 0;
DeflateStream dfsDecompress = new DeflateStream(MS,
CompressionMode.Decompress, false);
dsAfterDecompress.ReadXml(MS);
Thanks for any help,
Asaf

Feb 23 '06 #1
10 17791
Asaf,
your code seems to give the impression that you think a dataset,after being
compressed ,is "still a dataset". It is not.

Also, you will be better served by using the binary format which is much
more compact than xml.
Here is a short article with code that illustrates the technique:

http://www.eggheadcafe.com/articles/20060220.asp

Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Asaf" wrote:
Hi,

I am trying to Compress & Decompress a DataSet.
When running this code I am receiving the error:

System.Xml.XmlException was unhandled
Message="Root element is missing."
Source="System.Xml"

The code:

DataSet dsDataOrginal = new DataSet();
DataSet dsDataAfterDecompress = new DataSet();
dsDataOrginal.ReadXml(@"C:\VSProjects2005\ZIP_Comp ress\TheXML.XML");

//Compressing to Memory
MemoryStream MS = new MemoryStream();
DeflateStream dfs = new DeflateStream(MS,
CompressionMode.Compress, false);
dsDataOrginal.WriteXml(ms, XmlWriteMode.WriteSchema);

//DeCompressing from Memory
MS.Position = 0;
DeflateStream dfsDecompress = new DeflateStream(MS,
CompressionMode.Decompress, false);
dsAfterDecompress.ReadXml(MS);
Thanks for any help,
Asaf

Feb 23 '06 #2
Asaf <AG**@newsgroups.nospam> wrote:
I am trying to Compress & Decompress a DataSet.
When running this code I am receiving the error:

System.Xml.XmlException was unhandled
Message="Root element is missing."
Source="System.Xml"

The code:

DataSet dsDataOrginal = new DataSet();
DataSet dsDataAfterDecompress = new DataSet();
dsDataOrginal.ReadXml(@"C:\VSProjects2005\ZIP_Comp ress\TheXML.XML");

//Compressing to Memory
MemoryStream MS = new MemoryStream();
DeflateStream dfs = new DeflateStream(MS,
CompressionMode.Compress, false);
dsDataOrginal.WriteXml(ms, XmlWriteMode.WriteSchema);

//DeCompressing from Memory
MS.Position = 0;
DeflateStream dfsDecompress = new DeflateStream(MS,
CompressionMode.Decompress, false);
dsAfterDecompress.ReadXml(MS);


You're never using the deflating or inflating streams. You should be
calling
WriteXml (dfs, XmlWriteMode.WriteSchema).

You should then Flush dfs before using the MemoryStream. After that,
you should be able to call ReadXml but with dfsDecompress rather than
MS.

I'm slightly concerned that without having closed the DeflateStream to
start with, there may be problems. You may need to close dfs instead of
just flushing it, then create a new MemoryStream with the byte array
from the first MemoryStream, just so it knows where it ends properly.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 24 '06 #3
Hi Peter, Hi Jon,

Thanks for your reply.

Let's start again :-)
The code below is working fine when using FileStream:

//Compress to and save the file to disk
FileStream msf = new
FileStream(@"C:\VSProjects2005\ZIP_Compress\TheXML .xmd",
FileMode.Create, FileAccess.Write);
dfs = new DeflateStream(msf, CompressionMode.Compress, false);
dsOrginal.WriteXml(dfs);

dfs.Close();
msf.Close();

MessageBox.Show("Compress to file, Done!");

//Read Compressed file from disk, decompress data and show rows
count for dataset
FileStream infile = new
FileStream(@"C:\VSProjects2005\ZIP_Compress\TheXML .xmd", FileMode.Open,
FileAccess.Read);
DeflateStream DefStream = new DeflateStream(infile,
CompressionMode.Decompress, false);
dsAfterDecompress.ReadXml(DefStream);
infile.Close();

textBox1.Text = dsAfterDecompress.Tables[0].Rows.Count.ToString();
Same code but using MemoryStream does not work:

DataSet dsDataOrginal = new DataSet();
DataSet dsDataAfterDecompress = new DataSet();
dsDataOrginal.ReadXml(@"C:\VSProjects2005\ZIP_Comp ress\TheXML.XML");

//Compressing to Memory
MemoryStream MS = new MemoryStream();
DeflateStream dfs = new DeflateStream(MS,
CompressionMode.Compress, false);
dsDataOrginal.WriteXml(dfs);
dfs.Close();

//DeCompressing from Memory
DeflateStream dfsDecompress = new DeflateStream(MS,
CompressionMode.Decompress, false);
dsAfterDecompress.ReadXml(dfsDecompress);

textBox1.Text = dsAfterDecompress.Tables[0].Rows.Count.ToString();

If using dfs.Close(); then I am getting the error:
"The base stream is not readable.
Parameter name: stream"

If dfs.Close(); is omitted then I am getting the error:
"Root element is missing" when trying to do:
dsAfterDecompress.ReadXml(dfsDecompress); my guess is because the Stream is
at the end Position but Position property can't be use with DeflateStream so
I can't rewind the DeflateStream to zero position before doing
dsAfterDecompress.ReadXml(dfsDecompress);

Help Please!

Regards,
Asaf

"Jon Skeet [C# MVP]" wrote:
Asaf <AG**@newsgroups.nospam> wrote:
I am trying to Compress & Decompress a DataSet.
When running this code I am receiving the error:

System.Xml.XmlException was unhandled
Message="Root element is missing."
Source="System.Xml"

The code:

DataSet dsDataOrginal = new DataSet();
DataSet dsDataAfterDecompress = new DataSet();
dsDataOrginal.ReadXml(@"C:\VSProjects2005\ZIP_Comp ress\TheXML.XML");

//Compressing to Memory
MemoryStream MS = new MemoryStream();
DeflateStream dfs = new DeflateStream(MS,
CompressionMode.Compress, false);
dsDataOrginal.WriteXml(ms, XmlWriteMode.WriteSchema);

//DeCompressing from Memory
MS.Position = 0;
DeflateStream dfsDecompress = new DeflateStream(MS,
CompressionMode.Decompress, false);
dsAfterDecompress.ReadXml(MS);


You're never using the deflating or inflating streams. You should be
calling
WriteXml (dfs, XmlWriteMode.WriteSchema).

You should then Flush dfs before using the MemoryStream. After that,
you should be able to call ReadXml but with dfsDecompress rather than
MS.

I'm slightly concerned that without having closed the DeflateStream to
start with, there may be problems. You may need to close dfs instead of
just flushing it, then create a new MemoryStream with the byte array
from the first MemoryStream, just so it knows where it ends properly.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Feb 24 '06 #4
In the file code you use two separate streams, which means the position is
effictively reset between the two methods; in the second block you are using
a single stream, but are *not* resetting the position.

Just set Position = 0 before trying to read what you have written

Marc
Feb 24 '06 #5
Hi Marc,

Position property of DeflateStream cannot be set.

Regards,
Asaf

"Marc Gravell" wrote:
In the file code you use two separate streams, which means the position is
effictively reset between the two methods; in the second block you are using
a single stream, but are *not* resetting the position.

Just set Position = 0 before trying to read what you have written

Marc

Feb 24 '06 #6
I should have been specific; the memory stream (MS) needs to be reset - i.e.

// ...blah
dfs.Close();

MS.Position = 0; // reset the memory stream so we can read our glorious
compressed data

//DeCompressing from Memory
DeflateStream dfsDecompress = new DeflateStream(MS,
CompressionMode.Decompress, false);
// blah...
=====

Note also that you wight want to wrap each deflating stream in a "using"
construct to ensure they are always disposed; and *technically* even
MemoryStream is disposable, so you might want to be "using" that as well -
but I don't want to get into *that* argument again, so do whatever you want
;-p

Marc
Feb 24 '06 #7
Hi Marc,

Now I am getting the error: "Cannot access a closed Stream."
Here is the complete code:

DataSet dsDataOrginal = new DataSet();
DataSet dsDataAfterDecompress = new DataSet();
dsDataOrginal.ReadXml(@"C:\VSProjects2005\ZIP_Comp ress\TheXML.XML");

//Compressing to Memory
MemoryStream MS = new MemoryStream();
DeflateStream dfs = new DeflateStream(MS,
CompressionMode.Compress, false);
dsDataOrginal.WriteXml(dfs);
dfs.Close();

//DeCompressing from Memory
MS.Position = 0;
DeflateStream dfsDecompress = new DeflateStream(MS,
CompressionMode.Decompress, false);
dsAfterDecompress.ReadXml(dfsDecompress);

textBox1.Text = dsAfterDecompress.Tables[0].Rows.Count.ToString();

Regards,
Asaf

"Marc Gravell" wrote:
I should have been specific; the memory stream (MS) needs to be reset - i.e.

// ...blah
dfs.Close();

MS.Position = 0; // reset the memory stream so we can read our glorious
compressed data

//DeCompressing from Memory
DeflateStream dfsDecompress = new DeflateStream(MS,
CompressionMode.Decompress, false);
// blah...
=====

Note also that you wight want to wrap each deflating stream in a "using"
construct to ensure they are always disposed; and *technically* even
MemoryStream is disposable, so you might want to be "using" that as well -
but I don't want to get into *that* argument again, so do whatever you want
;-p

Marc

Feb 24 '06 #8
Asaf wrote:
Now I am getting the error: "Cannot access a closed Stream."


Use MemoryStream.GetBuffer() to get the byte array, and create a new
MemoryStream using that byte array, specifying the length as the Length
of the old stream.

Apologies if the names are slightly wrong - I'm not on a Windows box at
the moment to check.

Jon

Feb 24 '06 #9
OK - look at the last parameter to the DeflateStream ctor; it is
"leaveOpen"; if you change this to true, it will leave your base-stream
open, and life will be good. By default responsibility for the base stream
is handed to the wrapper (or reader/writer) - but in this case the
base-stream must outlive the wrapper - so we need to change this setting.

The following (similar to yours, but using serialization as a simpler test)
works "as is":

static void Main(string[] args) {

string final, original = "Some object that I want to compress,
in my case a string via serialization - but could be anything that writes to
a stream";

System.Runtime.Serialization.IFormatter formatter = new
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter();
Console.WriteLine(original);
using (MemoryStream baseStream = new MemoryStream()) {
using (DeflateStream compress = new
DeflateStream(baseStream, CompressionMode.Compress, true)) {
formatter.Serialize(compress, original);
}
WriteArray("Compressed", baseStream.ToArray());
baseStream.Position = 0;
using (DeflateStream decompress = new
DeflateStream(baseStream, CompressionMode.Decompress, true)) {
final = (string) formatter.Deserialize(decompress);
}
}
Console.WriteLine(final);
}

static void WriteArray(string caption, byte[] data) {
string underline = new string('=', caption.Length);
Console.WriteLine(underline);
Console.WriteLine(caption);
Console.WriteLine(data.Length + " byte(s)");
Console.WriteLine(underline);
foreach (byte b in data) {
Console.Write(b.ToString("x2") + " ");
}
Console.WriteLine();
}
Feb 24 '06 #10
Hi,

Since DeflateStream is owning the MemoryStream when compressing, the
argument "LeaveOpen" must be set to true, otherwise DeflateStream will close
(and flush) the MemoryStream automatically after writing the compressed bytes
to it.
Setting false to "LeaveOpen" argument is a must when using FileStream to
close the file that DeflateStream will write the bytes to it.

Now I am able to compress and decompress to MemoryStream :-)

Thanks you all for your help and share of knowledge!

Regards,
Asaf
"Marc Gravell" wrote:
OK - look at the last parameter to the DeflateStream ctor; it is
"leaveOpen"; if you change this to true, it will leave your base-stream
open, and life will be good. By default responsibility for the base stream
is handed to the wrapper (or reader/writer) - but in this case the
base-stream must outlive the wrapper - so we need to change this setting.

The following (similar to yours, but using serialization as a simpler test)
works "as is":

static void Main(string[] args) {

string final, original = "Some object that I want to compress,
in my case a string via serialization - but could be anything that writes to
a stream";

System.Runtime.Serialization.IFormatter formatter = new
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter();
Console.WriteLine(original);
using (MemoryStream baseStream = new MemoryStream()) {
using (DeflateStream compress = new
DeflateStream(baseStream, CompressionMode.Compress, true)) {
formatter.Serialize(compress, original);
}
WriteArray("Compressed", baseStream.ToArray());
baseStream.Position = 0;
using (DeflateStream decompress = new
DeflateStream(baseStream, CompressionMode.Decompress, true)) {
final = (string) formatter.Deserialize(decompress);
}
}
Console.WriteLine(final);
}

static void WriteArray(string caption, byte[] data) {
string underline = new string('=', caption.Length);
Console.WriteLine(underline);
Console.WriteLine(caption);
Console.WriteLine(data.Length + " byte(s)");
Console.WriteLine(underline);
foreach (byte b in data) {
Console.Write(b.ToString("x2") + " ");
}
Console.WriteLine();
}

Feb 24 '06 #11

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

Similar topics

3
by: Vlki | last post by:
Hello, I can't find a way to send printer specific language codes to printer. Part of my code : String sString = "B50,0,0,3,1,2,50,B," + "12345678901234567890"; Font fFont = new Font("Arial",...
1
by: Neil West | last post by:
I’m trying to drag & drop listview items between two instances of my app. The actual data that’s passed in DoDragDrop is an arraylist that’s been serialized to a memorystream. The contents...
2
by: Brent Rogers | last post by:
I am having trouble with the DeflateStream.Read() method. For some reason it wants to return Zero. I have this set of classes: =================== using System; using...
12
by: InvalidLastName | last post by:
We have been used XslTransform. .NET 1.1, for transform XML document, Dataset with xsl to HTML. Some of these html contents contain javascript and links. For example: // javascript if (a &gt; b)...
2
by: Flpit | last post by:
Hi, Can anybody tell me how to use the System.IO.Compression Deflatestream Class. I have a byte which is a compressed set of bytes that form a block. I want to inflate the byte and return...
2
by: Random | last post by:
Why, oh why, won't my XmlTextWriter write properly to the MemoryStream. Or, why can't I read the xml back out of the MemoryStream! I can't think of a thing.... Dim reader As XmlReader Dim...
1
by: bthetford | last post by:
I am trying to roll a simple compression scheme for network communication in an app I am developing. On the sender side, I compress each block of a set amount of bytes individually, then send it...
4
by: =?Utf-8?B?Sm9uIEphY29icw==?= | last post by:
The compression part of this code works fine for me, but the decompression part of this code does not. The output file from that is the same as the compressed file. What is wrong with my code?...
1
by: sedwick | last post by:
I'm using .NET 2.0.50727 in VS 2005 Pro, ENU Service Pack 1 (KB926601). I've been experimenting with the System.IO.Compression classes GZipStream and DeflateStream, and I found it interesting to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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,...
0
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...

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.