Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am developing mobile application using c#.net and want to read from csv file. I have written a code as below but still not getting the individual values.

C#
private void readtext()
        {
            var reader = new StreamReader(File.OpenRead("\\Application\\Master.csv"));
            var  listA = new List<String>();
            var  listB = new List<String>();
            var  listC = new List<String>();
            var  listD = new List<String>();
            string vara1, vara2, vara3, vara4;
            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                var values = line.Split(',');

                listA.Add(values[0]);
                listB.Add(values[1]);
                listC.Add(values[2]);
                listD.Add(values[3]);
            }
            var firstlistA = listA.ToArray();
            var firstlistB = listB.ToArray();
            var firstlistC = listC.ToArray();
            var firstlistD = listD.ToArray();
        }


Can anyone help me
Posted
Updated 18-May-18 0:00am
v3
Comments
Manas Bhardwaj 14-May-12 6:11am    
Depends more on the data you have in csv. Did you try to debug?
sjelen 14-May-12 6:11am    
I don't see a problem here. Can you provide a sample of csv file? A bit more details on the problem?
Saumyavemula 14-May-12 6:41am    
The problem is I am getting the value of firstlistA as 'String[9]' but I want the value which is in my CSV file

With a couple of error checks in place, it works for me on a test CSV file:
C#
{
StreamReader reader = new StreamReader(File.OpenRead(@"D:\Temp\ContactsShort.csv"));
List<string> listA = new List<String>();
List<string> listB = new List<String>();
List<string> listC = new List<String>();
List<string> listD = new List<String>();
//string vara1, vara2, vara3, vara4;
while (!reader.EndOfStream)
    {
    string line = reader.ReadLine();
    if (!String.IsNullOrWhiteSpace(line))
        {
        string[] values = line.Split(',');
        if (values.Length >= 4)
            {
            listA.Add(values[0]);
            listB.Add(values[1]);
            listC.Add(values[2]);
            listD.Add(values[3]);
            }
        }
    }
string[] firstlistA = listA.ToArray();
string[] firstlistB = listB.ToArray();
string[] firstlistC = listC.ToArray();
string[] firstlistD = listD.ToArray();
}
So, I would start looking at your CSV file, and at the output you actually get.

BTW: Don't use var as a declaration all the time - it just shows you are too lazy to think about your datatypes. Particularly as Intellisense will fill out the right hand side for you in most case if you do the left...
 
Share this answer
 
Comments
Saumyavemula 14-May-12 6:37am    
Hey with this I am getting the value of firstlistA as 'String[9]' but I want the value which is in my CSV file
OriginalGriff 14-May-12 6:48am    
Then you need to put a breakpoint in your code and look at what value is loaded into "line", and then into "values" each time round the loop.
Saumyavemula 14-May-12 6:53am    
the entire row which is in csv file (i.e. col1,col2,col3,col4) is loaded in 'line' and in 'values' value is 'string[9]' but i want as 'col1'
OriginalGriff 14-May-12 7:08am    
Yes? That is what I would expect. "values" is an array, so it's value is "an array of 9 strings" which is what the debugger is telling you. If you press the "+" symbol beside the name "values" in the debugger, it will expand the actual string entries.
Maciej Los 14-May-12 7:30am    
Good answer, 5!
Try this:

C#
Using System.IO;

StreamReader sr = new StreamReader(@"file.csv");
// for set encoding
// StreamReader sr = new StreamReader(@"file.csv", Encoding.GetEncoding(1250));

string strline = "";
string[] _values = null;
int x = 0;
while (!sr.EndOfStream)
{
    x++;
    strline = sr.ReadLine();
    _values = strline.Split(';');
    if (_values.Length >= 6 && _values[0].Trim().Length>0)
    {
      MessageBox.show(_values[1]);
    }
}
sr.Close();


also refer some threads:

A Fast CSV Reader[^]

Building a Simple CSV Parser in C#[^]
C#.NET- Reading CSV file[^]
 
Share this answer
 
Comments
Maciej Los 14-May-12 7:25am    
Good answer, 5!
Prasad_Kulkarni 14-May-12 7:31am    
Thank you Isomac!
BobJanova 14-May-12 8:37am    
I use the LumenWorks one (the first link you posted), it is awesome and has a nice permissive licence. Reading a CSV properly is hard (the spec is not particularly clear and quotes are awkward) and something ideally suited to farming out to a third party library.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900