Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从url读取csv文件?_C#_Csv - Fatal编程技术网

C# 如何从url读取csv文件?

C# 如何从url读取csv文件?,c#,csv,C#,Csv,我正在尝试创建一个web服务,它可以访问URL,例如www.domain.co.uk/prices.csv,然后读取csv文件。这可能吗?如何实现?理想情况下不下载csv文件 // Download the file to a specified path. Using the WebClient class we can download // files directly from a provided url, like in this case. System.Net.WebClie

我正在尝试创建一个web服务,它可以访问URL,例如www.domain.co.uk/prices.csv,然后读取csv文件。这可能吗?如何实现?理想情况下不下载csv文件

// Download the file to a specified path. Using the WebClient class we can download 
// files directly from a provided url, like in this case.

System.Net.WebClient client = new WebClient();
client.DownloadFile(url, csvPath);

url是包含csv文件的站点,csvPath是您希望实际文件的位置。

在Web服务中,您可以使用WebClient类下载文件,类似这样的内容(我没有进行任何异常处理,没有使用或关闭/处置调用,只是想让您了解如何使用和优化/改进…)

一旦文件内容在服务的执行流中可用,您就可以使用它做任何您喜欢的事情

如果必须将其作为web服务调用的返回值返回给客户端,则可以返回
数据集或任何其他您喜欢的数据结构。

的文档中有一个使用流的示例。使用流可以解析文档,而无需将其全部存储在内存中

您可以使用:

public string GetCSV(string url)
{
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

    StreamReader sr = new StreamReader(resp.GetResponseStream());
    string results = sr.ReadToEnd();
    sr.Close();

    return results;
} 
然后将其拆分:

public static void SplitCSV()
{
    List<string> splitted = new List<string>();
    string fileList = getCSV("http://www.google.com");
    string[] tempStr;

    tempStr = fileList.Split(',');

    foreach (string item in tempStr)
    {
        if (!string.IsNullOrWhiteSpace(item))
        {
            splitted.Add(item);
        }
    }
}
publicstaticvoidsplitcsv()
{
列表拆分=新列表();
字符串文件列表=getCSV(“http://www.google.com");
字符串[]tempStr;
tempStr=fileList.Split(',');
foreach(tempStr中的字符串项)
{
如果(!string.IsNullOrWhiteSpace(项))
{
拆分。添加(项目);
}
}
}
尽管有很多CSV解析器,我建议不要使用自己的。是一个很好的构造函数。

有一个接受流的构造函数

如果您决定使用此选项,您的示例将是:

void GetCSVFromRemoteUrl(string url)
{
    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
    HttpWebResponse response = request.GetResponse() as HttpWebResponse;

    using (CsvReader csvReader = new CsvReader(response.GetResponseStream(), true))
    {
        int fieldCount = csvReader.FieldCount;
        string[] headers = csvReader.GetFieldHeaders();

        while (csvReader.ReadNextRecord())
        {
            //Do work with CSV file data here
        }
    }

}

还允许您直接从流中读取。

而无需下载CSV文件?你希望如何阅读它?或者你只是说不必先下载整个文件就可以读取其中的一部分。你想做客户端还是服务器端?下载并不意味着你必须将文件保存到磁盘上,如果你这么认为的话。尽管如此,不下载文件就无法读取该文件。如果要将整个文件读入字符串,并且不需要
WebRequest
的额外灵活性,则可以使用
WebClient.DownloadString
。如果使用
VisualBasic.IO.TextFieldParser
,只需添加对Microsoft.VisualBasic的引用。之后,请使用此选项,而不是流读取器<代码>使用(TextFieldParser parser=newtextfieldparser(response.GetResponseStream())
hmmmm所以如果我想将csv放入数据表,我会做datatable=webClient吗
void GetCSVFromRemoteUrl(string url)
{
    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
    HttpWebResponse response = request.GetResponse() as HttpWebResponse;

    using (CsvReader csvReader = new CsvReader(response.GetResponseStream(), true))
    {
        int fieldCount = csvReader.FieldCount;
        string[] headers = csvReader.GetFieldHeaders();

        while (csvReader.ReadNextRecord())
        {
            //Do work with CSV file data here
        }
    }

}