0

I'm having a question about writing data to a CSV file. I have a file named test.csv in which are 2 fields > accountnumber and relation ID. Now I want to add another field next to it: IBAN. The IBAN is the data from the first row which is validated by the SOAP function BBANtoIBAN.

How can I keep the 2 rows of data accountnumbers and relation IDs in the CSV and add the IBAN in the 3rd row?

This is my code so far:

using (var client = new WebService.BANBICSoapClient("IBANBICSoap"))
{
    List<List<string>> dataList = new List<List<string>>();
    TextFieldParser parser = new TextFieldParser(@"C:\CSV\test.csv");
    parser.TextFieldType = FieldType.Delimited;
    parser.SetDelimiters(";");
    while (!parser.EndOfData)
    {
        List<string> data = new List<string>();
        string row = parser.ReadLine();

        try
        {
            string resultIBAN = client.BBANtoIBAN(row);
            if (resultIBAN != string.Empty)
               data.Add(resultIBAN);
            else
               data.Add("Accountnumber is not correct.");
        }
        catch (Exception msg)
        {
            Console.WriteLine(msg);
        }
        dataList.Add(data);
    }  
}
2
  • 1
    You can read file and each line add ";[yourIBANPropValue]" Commented Oct 10, 2013 at 7:08
  • How do you mean, in parser.Readline()? Is there any documentation or examples online so I can take a look at it? Commented Oct 10, 2013 at 7:09

2 Answers 2

2

I see it as:

StreamReader sr = new StreamReader(@"C:\CSV\test.csv")
StreamWriter sw = new StreamWriter(@"C:\CSV\testOut.csv")
while (sr.Peek() >= 0) 
{
    string line = sr.ReadLine(); 


try
{
       string[] rowsArray = line.Split(';');
       string row = rowsArray[0];
       string resultIBAN = client.BBANtoIBAN(row);
       if (resultIBAN != string.Empty)
       {
           line +=";"+ resultIBAN;
       }
       else
       {
            line +=";"+"Accountnumber is not correct.";
       }

 }
 catch (Exception msg)
 {
     Console.WriteLine(msg);
 }
 sw.WriteLine(line) 
 }
sr.Close();
sw.Close();
Sign up to request clarification or add additional context in comments.

9 Comments

row is in this case line I guess?
Hm, I'll add some additional info, I forgot some things
The testOut.csv is created; but empty.
Are you add sr.Close() and sw.Close() ?
And it`s still empty? Have you message about exceptions?
|
0

I would do something like this to parse the csv file, and add an extra item to the data list:

        List<List<string>> dataList = new List<List<string>>();
        string filename = @"C:\CSV\test.csv";
        using (StreamReader sr = new StreamReader(filename))
        {
            string fileContent = sr.ReadToEnd();

            foreach (string line in fileContent.Split(new string[] {Environment.NewLine},StringSplitOptions.RemoveEmptyEntries))
            {
                List<string> data = new List<string>();
                foreach (string field in line.Split(';'))
                {
                    data.Add(field);
                }
                try
                {
                    string resultIBAN = client.BBANtoIBAN(data[0]);
                    if (resultIBAN != string.Empty)
                    {
                        data.Add(resultIBAN);
                    }
                    else
                    {
                        data.Add("Accountnumber is not correct.");
                    }
                }
                catch (Exception msg)
                {
                    Console.WriteLine(msg);
                }
                dataList.Add(data);
            }

2 Comments

Then 'row' in this case is 'field'? How can I validate the value of the first row into the function BBANtoIBAN and then add a 3rd row with the output of the function?
Sorry, didn't catch that. You would pass in the first string in the List.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.