[英]Read and process a remote zipped xml file in C#
在 C# 中读取和处理远程压缩 xml 文件的正确方法是什么?
这是我的尝试:
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;
}
在这个问题中提出了解决方案。
对我来说,一旦到达using (var file = File.OpenRead(url))
行,解决方案就会出错。 错误说明如下:
{"不支持给定路径的格式。"}
我应该改变什么才能使解决方案起作用?
问题在这里: https://srv-file7.gofile.io/download/k67HY4/sampleOpenData.zip
。 File
类仅适用于本地文件。 首先,您必须将此文件下载到本地存储,然后打开它。 例如使用这个答案中的解决方案。
直接内存解决方案:
WebClient wc = new WebClient();
using (MemoryStream stream = new MemoryStream(wc.DownloadData("URL")))
{
using (var zip = new ZipArchive(stream, ZipArchiveMode.Read))
{
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.