by costamesakid » Wed Apr 28, 2010 4:53 am
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[i] = csvData[j].Split(';')[i];
}
//if (csvData[j].StartsWith['#'])
dt.Rows.Add(row);
}
}
catch (Exception e)
{
Console.WriteLine("{0} Parse CSV Data Error: ", e.ToString());
}
}
}