C# read CSV file from URL without downloading


20/08/2021- duocnt    1581 Views    

STEPS TO DO

  1. Create console application.
  2. Create a class with the name ReadCsvOnline.
  3. Read CSV from URL.
  4. Result.

Source code.

https://github.com/ntduoc/ReadCSVFileFromURLWithoutDownload.git

Action.

1 - Create a console application.


2 - Create a class with the name ReadScvOnline.


  • Create Constructor method and URL properties for the class.


  • Create a method for reading the header of CSV.
        public string[] CsvHeaders()
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            StreamReader sr = new StreamReader(resp.GetResponseStream());
            string[] headers;
            headers = sr.ReadLine().Split('\t');
            return headers;
        }


  • Create a method for reading the content of CSV.
        public StringBuilder CsvContent()
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            StreamReader sr = new StreamReader(resp.GetResponseStream());
            StringBuilder stringBuilder = new StringBuilder();
 
            sr.ReadLine();
            string textline;
            while ((textline = sr.ReadLine()) != null)
            {
                stringBuilder.Append(textline);
                stringBuilder.Append(";");               
            }
            return stringBuilder;
        }


3 - Read SCV from URL.

  • Create an instance of class ReadCsvOnline and pass URL to URL properties.


  • Call method for reading the headers of CSV.


  • Call method for reading the content of CSV.


  • Full code for reading CSV.


4 - Result.

  • SCV Header result.


  • SCV Content result.

Comments

;
;