This is more of a C# question but Im going to post it on the Ranorex forum as well.
I have a test that reads in records from a CSV file to populate a form. The CSV file is exceedingly long so I was hoping to use comments to section it out. The problem is that there is no comment character standard for a CSV file, but I did hear that you can define your own comment character in the C# file that reads the CSV file. For instance "skip rows that start with '#'".
The C# file that I am actually using to read the CSV file is CSVConnector.cs which is packaged with Ranorex in the Data Driven sample. Is it possible to define a comment character in this file, and if so can I possibly get an example of the code?
Possibly this is something Ranorex can incorporate in there CSVConnector.cs file in future releases? Thanks
Defining Comment Character in CSV File
Re: Defining Comment Character in CSV File
I've not looked at the CSVConnector.cs file but you could do your own code very easily...
Somethign like this.
Somethign like this.
Code: Select all
string strFile1 = "myfile.txt";
using (StreamReader li = new StreamReader(strFile1))
{
while (true)
{
if (li.EndOfStream)
break;
string liTxt = li.ReadLine();
if (liTxt.StartsWith("#")
{
//skip line
}
else
{
//continue doing what you need
}
}
}
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!
Ciege...
Ciege...
-
- Posts: 94
- Joined: Tue Jun 16, 2009 10:27 pm
Re: Defining Comment Character in CSV File
Thanks Ciege.
I tried a few variations of this code but could not get it to work. Its a minor syntax error for sure so I pawned it off to one of our developers.
I tried a few variations of this code but could not get it to work. Its a minor syntax error for sure so I pawned it off to one of our developers.
Re: Defining Comment Character in CSV File
What error (if any) are you getting? Is it a compile error or a runtime error? Please add some more information so I can help out.
It wasn’t a direct copy/paste from my code base since I had to remove a bit of information before I was able to post the code so I may have fat-fingered something.
It wasn’t a direct copy/paste from my code base since I had to remove a bit of information before I was able to post the code so I may have fat-fingered something.
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!
Ciege...
Ciege...
-
- Posts: 94
- Joined: Tue Jun 16, 2009 10:27 pm
Re: Defining Comment Character in CSV File
Well... I have been assigned to re-investigate this issue and non of my attempts to implement Ciege's code are working. For starters, I have no idea where to implement Ciege's code within the current CSV Connector file. I also cannot find the C# syntax to skip a line that begins with a certain character. I have pasted the Ranorex CSV Connector code below. If I could get a little more direction with this issue it would be greatly appreciated. It just seems like this should be a much easier then I am making it out to be. I really would have figured if the '.Split' method defines the column character then there certainly should be a method to define the comment character?
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
/// <summary>
/// Represents a data connector to a CSV-file
/// </summary>
class CSVConnector
{
private string fileName = null;
private DataTable dt = null;
/// <summary>
/// Initializes a new instance of the <see cref="CSVConnector"/> class.
/// </summary>
/// <param name="fileName">The path to the CSV file <see cref="CSVConnector"/></param>
public CSVConnector(String fileName)
{
try
{
if (fileName == null)
return;
this.fileName = fileName;
dt = new DataTable();
ParseCSVData();
}
catch (Exception e)
{
Console.WriteLine("{0} Open Connector Error: ", e.ToString());
}
}
/// <summary>
/// Gets the Data Collection Rows
/// </summary>
public DataRowCollection Rows
{
get { return dt.Rows; }
}
/// <summary>
/// Gets the Data Columns
/// </summary>
public DataColumnCollection Header
{
get { return dt.Columns; }
}
private void ParseCSVData()
{
try
{
String[] csvData = System.IO.File.ReadAllLines(fileName);
if (csvData.Length == 0)
return;
String[] headings = csvData[0].Split(';');
foreach (string header in headings)
{
dt.Columns.Add(header, typeof(string));
}
for (int j = 1; j < csvData.Length; j++)
{
DataRow row = dt.NewRow();
for (int i = 0; i < headings.Length; i++)
{
row = csvData[j].Split(';');
}
//if (csvData[j].StartsWith['#'])
dt.Rows.Add(row);
}
}
catch (Exception e)
{
Console.WriteLine("{0} Parse CSV Data Error: ", e.ToString());
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
/// <summary>
/// Represents a data connector to a CSV-file
/// </summary>
class CSVConnector
{
private string fileName = null;
private DataTable dt = null;
/// <summary>
/// Initializes a new instance of the <see cref="CSVConnector"/> class.
/// </summary>
/// <param name="fileName">The path to the CSV file <see cref="CSVConnector"/></param>
public CSVConnector(String fileName)
{
try
{
if (fileName == null)
return;
this.fileName = fileName;
dt = new DataTable();
ParseCSVData();
}
catch (Exception e)
{
Console.WriteLine("{0} Open Connector Error: ", e.ToString());
}
}
/// <summary>
/// Gets the Data Collection Rows
/// </summary>
public DataRowCollection Rows
{
get { return dt.Rows; }
}
/// <summary>
/// Gets the Data Columns
/// </summary>
public DataColumnCollection Header
{
get { return dt.Columns; }
}
private void ParseCSVData()
{
try
{
String[] csvData = System.IO.File.ReadAllLines(fileName);
if (csvData.Length == 0)
return;
String[] headings = csvData[0].Split(';');
foreach (string header in headings)
{
dt.Columns.Add(header, typeof(string));
}
for (int j = 1; j < csvData.Length; j++)
{
DataRow row = dt.NewRow();
for (int i = 0; i < headings.Length; i++)
{
row = csvData[j].Split(';');
}
//if (csvData[j].StartsWith['#'])
dt.Rows.Add(row);
}
}
catch (Exception e)
{
Console.WriteLine("{0} Parse CSV Data Error: ", e.ToString());
}
}
}
- Support Team
- Site Admin
- Posts: 12145
- Joined: Fri Jul 07, 2006 4:30 pm
- Location: Houston, Texas, USA
- Contact:
Re: Defining Comment Character in CSV File
Hi!
IMHO, it would be more usefull to place the if statement inside the for loop.
Regards,
Peter
Ranorex Support Team
IMHO, it would be more usefull to place the if statement inside the for loop.
for (int j = 1; j < csvData.Length; j++) { DataRow row = dt.NewRow(); //If row does not start with "#" go into the if statement and execute code inside. if(!csvData[j].StartsWith("#")) { for (int i = 0; i < headings.Length; i++) { row = csvData[j].Split(';'); } dt.Rows.Add(row); } }
Regards,
Peter
Ranorex Support Team
-
- Posts: 94
- Joined: Tue Jun 16, 2009 10:27 pm
Re: Defining Comment Character in CSV File
Thanks Peter
This works perfect for what I need the code to do.
This works perfect for what I need the code to do.