Hi
I'm trying to read a CSV file that looks something like this:
A;1;;3;4;5;6;7;;8
B;1;;3
C;;;;
I'm using Nav 2015.
My goal is to be able to do the reading via DotNet. My idea is as follows:
1. Read the file line by line
2. For each line I'll split (by using 'Split' command) the line with ';' as separator, and store the content in a DotNet array - the array will be of variable size, according to the number of ';'
3. Read the Dotnet array from (2) and store the values in a table.
This is what I've come up with until now:
OBJECT Codeunit 70010 Read CSV
{
OBJECT-PROPERTIES
{
Date=23-11-15;
Time=14:58:43;
Modified=Yes;
Version List=;
}
PROPERTIES
{
OnRun=VAR
InFile@1160180000 : File;
inStream@1160180001 : InStream;
streamReader@1160180002 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.StreamReader";
encoding@1160180003 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Text.Encoding";
String@1160180006 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.String";
CSVFields@1160180007 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Collections.Generic.List`1";
txt@1160180004 : Text;
FileManagement@1160180005 : Codeunit 419;
BEGIN
InFile.OPEN(FileManagement.UploadFileSilent('C:\Temp\filetoread.txt'));
InFile.CREATEINSTREAM(inStream);
streamReader := streamReader.StreamReader(inStream, encoding.Default);
WHILE NOT streamReader.EndOfStream DO BEGIN
String := streamReader.ReadLine();
//do something with split....
CSVFields := String.Split(';');
END;
END;
}
CODE
{
BEGIN
END.
}
}
But I'm missing the Array-part:-(
0
Comments
You could write a small assembly to do this for you in C#. Just return a generic list of a custom class and then loop through it in C/AL.
But honestly, why do do you want to do it like this? Why not an xmlport?
so why do you not to use the new possibilities of C/AL?
This example:
should do the same. And if put into a report, you may get the same behavior as the old dataports, with a requestpage where you may enter or select a filename before you start the import.
Regards Fiddi
There is nothing wrong with that at all. Don't get me wrong, I'm all for it.
But if there's a proven technology in the system that does everything you want while the new way has no added value and only brings extra complexity, you might think twice
Now you need some reflection to create a .net instance of type string
Now we use the MethodInfo object, to get the function from reflection:
After we have the method signature, we can use MethodInfo.Invoke to invoke the function. The return value is a string array.
strArray := MethodInfo.Invoke(newString, ArrOfValues);
Now you have a "|" delimited string array which you can easily iterate over with:
To make a conclusion what this code does:
We create a type array to determine our function signature. After that we call the GetMethod function, to get our Method by the signature we initially declared. In our type array.
After that, we invoke the function we just got by GetMethod.
Best regards