Parser procedure with file file

We have a flat file. The first five characters in each line determine which type the line belongs to. Each type has several fields, and all of them are of fixed length. How can I parse a file and save it in a database?

A couple of options:

1) Create an XML template for each type and based on the first five characters determine which template to use for parsing the string

.

<FileMap> <Field Name="FirstName" Start="0" Length="5"/> <Field Name="LastName" Start="5" Length="10"/> <Field Name="Address" Start="15" Length="15"/> <Field Name="City" Start="30" Length="14"/> <Field Name="State" Start="44" Length="2"/> <Field Name="Zip" Start="46" Length="5"/> </FileMap> 

2) Use the filesHelpers library ( http://www.filehelpers.net/ )

Any other suggestions? Please let me know

+4
source share
4 answers

The Microsoft.VisualBasic.FileIO.TextFieldParser class is very well versed in structured text files. You can use it with C #. Just add the link to Microsoft.VisualBasic.dll and the using Microsoft.VisualBasic.FileIO statement in your code.

+3
source

Simple IndexOf string, a substring will not be enough?

+2
source

One of the possible ways, perhaps not the best, but, as an example, use layout structures for this:

 ... using System.Runtime.InteropServices; [StructLayout(LayoutKind.Sequential)] public struct Type1 { [MarshalAs(UnmanagedType.ByValArray, SizeConst = 5)] public char[] FirstName; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)] public char[] LasteName; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 15)] public char[] Address; ... } 

And the use will be as follows:

 using (TextReader reader = File.OpenText(@"D:\flatfile.txt")) { string line = reader.ReadLine(); string code = line.Substring(0, 5); // Add your structures factory realization Type type = Factory.GetStructureByCode(code); string typeInitializtion = line.Substring(5, (line.Length - 5)); byte[] bytes = Encoding.UTF8.GetBytes(typeInitializtion); //Allocate amount of memoty IntPtr safePrt = Marshal.AllocCoTaskMem(Marshal.SizeOf(type)); //Copy 'bytes' byte buffer into memmory allocated Marshal.Copy(bytes, 0, safePrt, bytes.Length); //Map structure to pointer var myStructure = Marshal.PtrToStructure(safePrt, type); } 

You can find a good description of using layout structure here .

Btw, you can use strings instead of a char array as a property type with the following attribute:

  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 5)] public string FirstName; 

but in this case the last character of the line will be lost - it is replaced with '/ 0' (considered as a line with zero termination).

+1
source

In case you want to collapse yourself: associate each type with the Insert command, capture RegExp and map the RegExp group to the command parameter.

0
source

Source: https://habr.com/ru/post/1343379/


All Articles