From: Asaf on
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_Compress\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

From: Jon Skeet [C# MVP] on
Asaf <AG70(a)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_Compress\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 - <skeet(a)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
From: Asaf on
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_Compress\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 <AG70(a)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_Compress\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 - <skeet(a)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
>
From: Marc Gravell on
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


From: Asaf on
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
>
>
>