0

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?

BWA
  • 5,672
  • 7
  • 34
  • 45
hellouworld
  • 525
  • 6
  • 15

1 Answers1

1

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))
    {
       ...
    }
}
BWA
  • 5,672
  • 7
  • 34
  • 45
  • I am very grateful for your time and attention. But that is not what I am looking for. I do not want to store the file on disk, I want to process it in memory. Are there any other ways except using the `File` class? – hellouworld Feb 11 '20 at 13:53
  • File calss cannot read from https. You cannot save temp file and delete it after processing? – BWA Feb 11 '20 at 13:57
  • In the scope of this question I am not considering to preserve the file on disk. – hellouworld Feb 11 '20 at 13:59
  • @hellouworld I completed my answer with memory processing. Written from the head so it may need to be fixed. – BWA Feb 11 '20 at 14:02