hellouworld
hellouworld

Reputation: 605

Read and process a remote zipped xml file in C#

What is the correct way to read and process a remote zipped xml file in C#?

Here is what I try:

private Task UpdateLegalContractors(string url)
{
    url = @"https://srv-file7.gofile.io/download/k67HY4/sampleOpenData.zip";

    string res = string.Empty;

    using (var file = File.OpenRead(url))
    using (var zip = new ZipArchive(file, ZipArchiveMode.Read))
    {
        foreach (var entry in zip.Entries)
        {
            using (var stream = entry.Open())
            using (var reader = XmlReader.Create(stream))
            {
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            res += reader.Name;
                            break;
                        case XmlNodeType.XmlDeclaration:
                            res += "<?xml version=\"1.0\" encoding=\"windows - 1251\"?>";
                            break;
                    }
                }
            }
        }
    }

    var stop = 0;

    return null;
}

The solution was suggested in this question.

For me the solution gives an error once the using (var file = File.OpenRead(url)) line is reached. The error says the following:

{"The given path's format is not supported."}

enter image description here

What should I change for the solution to work?

Upvotes: 0

Views: 406

Answers (1)

BWA
BWA

Reputation: 5764

Problem is here: https://srv-file7.gofile.io/download/k67HY4/sampleOpenData.zip. File class works only with local files. First you must download this file to local storage and then open it. Eg using solution from this answer.

Direct in memory solution:

WebClient wc = new WebClient();
using (MemoryStream stream = new MemoryStream(wc.DownloadData("URL")))
{
    using (var zip = new ZipArchive(stream, ZipArchiveMode.Read))
    {
       ...
    }
}

Upvotes: 1

Related Questions