2011-12-11 129 views
0

可能重复:
Unable to open CSV file from internet using C#无法从网站打开CSV文件

我写了这个代码来打开并解析一个CSV文件。当我从我的硬盘驱动器上的文件夹打开文件时它工作。我更改了第24 - 26行中的代码以从网站打开文件。 我收到错误消息“不支持URI格式”从附加代码的第27行开始。我可以使用VBA打开文件。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 
using System.Net; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      // 
      // Read in a file line-by-line, and store it all in a List. 
      // 
      int i = 0; 
      DateTime dte; 
      List<string> list = new List<string>(); 
      float[] Prices = new float[4]; 

      WebClient wc = new WebClient(); 

      byte[] data = wc.DownloadData("http://www.datasource.com/apps/qt/csv/pricehistory.ac?section=yearly_price_download&code=XXX"); 

      using (StreamReader reader = new StreamReader(wc)) 
      { 
       string line; 
       while ((line = reader.ReadLine()) != null) 
       { 
        //list.Add(line); // Add to list. 
        Console.WriteLine(line); // Write to console. 

        string[] parts = line.Split(','); 
        int DateSetter = 1; 
        int DateDone = 0; 
        int CountFloat = 0; 
        int PricesDone = 0; 
        Double Volume = 0; 

        foreach (string part in parts) 
        { 
         Console.WriteLine("{0} : {1}", i, part); 

         if (DateSetter == 1) 
         { 
          dte = DateTime.Parse(part); 
          DateSetter = 2; 
          Console.WriteLine(dte); 
         } 
         if (DateDone == 1) 
         { 
          if (DateSetter < 6) 
          { 
           Prices[CountFloat] = float.Parse(part); 
           CountFloat++; 
           DateSetter++; 
           Console.WriteLine(Prices[3]); 
          } 
         } 
         DateDone = 1; 
         if (PricesDone == 1) 
         { 
          Volume = double.Parse(part); 
          Console.WriteLine(Volume); 
         } 
         if (DateSetter == 6) 
         { 
          PricesDone = 1; 
         } 
        } 



      } 
     } 
     Console.ReadLine(); 
    } 
} 

}

回答

0

只需更换

byte[] data = wc.DownloadData 

随着

String data = wc.DownloadString 

如果你的要求不是非常大。这将简化所有程序。