Updating a CSV file

Class library usage, coding and language questions.
EDR_CB
Posts: 17
Joined: Thu Mar 31, 2016 8:32 am

Updating a CSV file

Post by EDR_CB » Wed Dec 21, 2016 3:04 pm

Dear all,

i want to update a CSV file that i am parsing.

A quick search on the forum gave me this piece of code to parse my csv file :

Code: Select all

CsvDataConnector csvConnector = new Ranorex.Core.Data.CsvDataConnector("CSVConnector","Path to File",true);
            csvConnector.SeparatorChar = ';';
            ColumnCollection outColCollection;
            
            RowCollection outRowCollection;
            csvConnector.LoadData(out outColCollection, out outRowCollection);
            foreach(Ranorex.Core.Data.Row dataRow in outRowCollection)
            {
                Report.Info(""+dataRow["Column A"].ToString());
                Report.Info(""+dataRow["Column B"].ToString());
                Report.Info(""+dataRow["Column C"].ToString());                
			}
It works fine, but how can i update my csv file ?

Imagine my CSV file is as such :

Column A;Column B;Column C;
ValueA1;ValueB1;ValueC1;
ValueA2;ValueB2;ValueC2;
ValueA3;ValueB3;ValueC3;

How can i update the ValueB2 for example ?

Thank you in advance for you help !

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 3914
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Updating a CSV file

Post by odklizec » Wed Dec 21, 2016 3:18 pm

Hi,

Check for example this post, describing writing to CSV file using Ranorex CSV DataConnector:
http://www.ranorex.com/forum/writing-in ... tml#p34657
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

EDR_CB
Posts: 17
Joined: Thu Mar 31, 2016 8:32 am

Re: Updating a CSV file

Post by EDR_CB » Wed Dec 21, 2016 3:37 pm

Thank you for that quick answer.

The post you linked showed me that if a use the following code after the foreach :

Code: Select all

outRowCollection.Add(new string[3]{"val7","val8","val9"});  
csvConnector.StoreData(outColCollection, outRowCollection); 
It will add a row at the end of the CSV file.

But me i want to change a value that is in the middile of the csv file.

I tried putting these two ligns inside the foreach (just to see what will happen) but i get an error telling me i can't modify a collection while parsing it.

Any other ideas ?

User avatar
RobinHood42
Posts: 238
Joined: Fri Jan 09, 2015 3:24 pm

Re: Updating a CSV file

Post by RobinHood42 » Fri Dec 23, 2016 9:03 am

Hi EDR_CB

You could try the following:
//Connector name - CSVConnector
            Ranorex.Core.Data.DataCache myConn = DataSources.Get("CSVConnector");
			myConn.Load();
            
			Ranorex.Core.Data.ColumnCollection propTableColumnsCSV = myConn.Columns;
			Ranorex.Core.Data.RowCollection propTableRowsCSV = myConn.Rows;
			
			//Set the first column value for each row to "new value"
            	foreach (var row in propTableRowsCSV)
			{
            		
				row.Values[0] = "new value";
			}
			
            myConn.Store();
Hope this helps, :mrgreen:
Robin