By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,900 Members | 1,078 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,900 IT Pros & Developers. It's quick & easy.

DeflateStream & MemoryStream?

P: n/a
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
Share this Question
Share on Google+
10 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.