0

this one is baking my head, i need an XML feed, but the feed is inside a gzip file. now i can download, unpack, save file etc.

but what i want to do is get the gzip in memory put the contents into memory read the contents into memory and then pass that to the serializer

problem here is that results contain unreadable gibberish and reader1 and reader both contain none. I am missing another line here but cant seem to find what it is.

var request = (HttpWebRequest)HttpWebRequest.Create("urltofeed.com");
request.Credentials = CredentialCache.DefaultCredentials;
// auto unpack gzip
request.AutomaticDecompression = DecompressionMethods.GZip;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader data = new StreamReader(response.GetResponseStream()); 
var result = data.ReadToEnd();

XmlReader reader1 = XmlReader.Create(response.GetResponseStream());
var reader = new XmlTextReader(response.GetResponseStream());
sehe
  • 374,641
  • 47
  • 450
  • 633
davethecoder
  • 3,856
  • 4
  • 35
  • 66

2 Answers2

2

Direclty from MSDN:

            //Create the decompressed file.
            using (FileStream outFile = File.Create(origName))
            {
                using (GZipStream Decompress = new GZipStream(inFile,
                        CompressionMode.Decompress))
                {
                    // Copy the decompression stream 
                    // into the output file.
                    Decompress.CopyTo(outFile);

                    Console.WriteLine("Decompressed: {0}", fi.Name);

                }
            }

So in your case something like

using (var GZipStream decompress = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress))
{
    // XmlReader reader1 = XmlReader.Create(decompress);
    // or ? var reader = new XmlTextReader(decompress);
}
sehe
  • 374,641
  • 47
  • 450
  • 633
  • so what does request.AutomaticDecompression = DecompressionMethods.GZip; do if this does not decompress the results as i thought the results would be decompressed results I.E XML – davethecoder Nov 26 '11 at 20:06
  • @minus4: ah, missed that in the OP; My guess is that the mime type is not appropriate/a header is missing or the encoding isn't actually GZip (just guessing _really_) – sehe Nov 26 '11 at 20:11
  • nope its gzip, my code before was to download, unzip and read the XML i want to stream, and stream XML. P.S the code above does not work still empty – davethecoder Nov 26 '11 at 20:14
  • A server might compress the whole response as gzip, but to make the client decompress the response the client has to support it, and the server has to send the correct header to ensure that the client understand it's supposed to decompress – Onkelborg Nov 26 '11 at 20:21
  • @Onkelborg confused, not sure what your solution is here. i have no problems with the source, the source is good, i have been using the source, but i want to get it as a stream rather than onto disk and from disk then deleting files. ta – davethecoder Nov 26 '11 at 20:28
  • 1
    with your code sehe i added this: decompress.CopyTo(memoryStream); var dataTest = memoryStream.ToArray(); var dataTest2 = System.Text.Encoding.UTF8.GetString(dataTest); – davethecoder Nov 26 '11 at 20:41
  • seems to work cos i get a new error of invalid URI when reading XML but the data IS XML now..... thanks – davethecoder Nov 26 '11 at 20:42
0

You have to decompress the gzip-file, that step is missing. Try this: GZipStream and decompression

Community
  • 1
  • 1
Onkelborg
  • 3,927
  • 1
  • 19
  • 22