C#在互联网上下载多XML

时间:2018-08-11 05:45:38

标签: xml webclient

我正在从互联网上下载大约1000000个xml文件。

我已经尝试了很多方法,但是当我快速下载时,文件为空。否则会很慢... 我要如何做得更好 最好的问候...

WebClient client = new WebClient();
NetworkCredential myCreds = new NetworkCredential("user", "pass");
client.Credentials = myCreds;


         foreach (var id in ids)
                {
                    try
                    {
                        string url = "http://....../fileid=" + id;

                        client.Encoding = Encoding.UTF8;
         //  client.DownloadFileAsync(new Uri(url), "C:\\Data\\"+id+".xml");

                        string result= client.DownloadString(url);
                        WriteFile(id, result);
                    }
                    catch (Exception)   {   }
                }


     private void WriteFile(decimal id, string text)
            {
                path ="C:\\Data\\"+id+".xml";
                using (StreamWriter sw = File.CreateText(path))
                {
                    sw.WriteLine(text);
                }
            }

0 个答案:

没有答案