Brainfreeze 与 gzip 文件,包含我想要作为流的 xml 文件

发布于 2024-12-17 22:26:10 字数 732 浏览 0 评论 0原文

这个让我很头疼,我需要一个 XML feed,但 feed 位于 gzip 文件内。 现在我可以下载、解压、保存文件等,

但我想做的是将 gzip 获取到内存中 将内容放入内存 将内容读入内存 然后将其传递给序列化器,

这里的问题是结果包含不可读的乱码,而 reader1 和 reader 均不包含任何乱码。我在这里缺少另一行,但似乎无法找到它是什么。

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());

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());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寻找我们的幸福 2024-12-24 22:26:10

来自 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);

                }
            }

所以在你的情况下喜欢

using (var GZipStream decompress = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress))
{
    // XmlReader reader1 = XmlReader.Create(decompress);
    // or ? var reader = new XmlTextReader(decompress);
}

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);
}
烏雲後面有陽光 2024-12-24 22:26:10

您必须解压缩 gzip 文件,该步骤缺失。试试这个:GZipStream 和解压缩

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文