0

I am trying to use the .Net class Microsoft.VisualBasic.FileIO.TextFieldParser to read in a csv. The problem I am having is that the value of the variable netArray appears to be being set to a single string, without the values being split to separate array entries. Any idea as to why this would happen?

int counter, xppArrayLength;
str xppValue;
str arr[];
System.String[]     netArray;
System.String[]     delimeters = new System.String[1]();
Microsoft.VisualBasic.FileIO.TextFieldParser parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(myPath);

delimeters.SetValue(",",0);
parser.set_Delimiters(delimeters);
parser.set_HasFieldsEnclosedInQuotes(true);

netArray = parser.ReadLine();

while(netArray.get_Length())
{
    xppArrayLength = netArray.get_Length();
    for(counter = 1; counter <= xppArrayLength; counter++)
    {
        xppValue = netArray.GetValue(counter-1);
        arr[counter] = xppValue;

    }

    netArray = parser.ReadLine();

} }

Dilitante
  • 148
  • 9
  • 1
    Just to make sure, you are aware that there is class CommaTextIO in AX that allows you to read and write csv files? (http://stackoverflow.com/questions/4390940/reading-a-comma-seperated-values-csv-file-in-dynamics-ax) – FH-Inway May 15 '15 at 13:52
  • No I wasn't. Do you know if that class is able to ignore commas in quotes? – Dilitante May 15 '15 at 14:32
  • Yes, it is able to do that (see also https://community.dynamics.com/ax/f/33/p/66618/121484) – FH-Inway May 15 '15 at 14:41

1 Answers1

1

Because TextFieldParser.ReadLine() is supposed to return a single string (as documented here https://msdn.microsoft.com/en-us/library/6a8fts4w%28v=vs.90%29.aspx)

You should be doing parser.ReadFields() (https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser.readfields%28v=vs.110%29.aspx)

Also you've not set TextFieldType = FieldType.Delimited (as documented here https://msdn.microsoft.com/en-us/library/63k8k5sx%28v=vs.90%29.aspx) TextFieldType default value is FieldType.Delimited, so no need to set it.

tjleigh
  • 1,134
  • 1
  • 10
  • 23
  • Thanks. Incidentally the set_TextFieldType method is asking for an AX FieldType enum, which doesn't seem to be right – Dilitante May 15 '15 at 12:25