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