Wednesday, November 19, 2014

how Import fixed width txt file and its fields are described in a configutation xml file

Sounds like two task, 1 read the data 2 retrieve the field types from xml. High level you could use TextFieldParser class and set FieldWidth property to match your data in the text file. See the following for working with schema file.


I don't have a pre-done sample on TextFieldParser for field fields, only for comma delimited but it is a start



DataTable dt = new DataTable { TableName = "MyTable" };
dt.Columns.Add(new DataColumn { ColumnName = "FirstName", DataType = typeof(string) });
dt.Columns.Add(new DataColumn { ColumnName = "LastName", DataType = typeof(string) });

using (TextFieldParser parser = new TextFieldParser(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TextFile2.txt")))
{
parser.Delimiters = new string[] { "," };
while (!parser.EndOfData)
{
string[] LineData = parser.ReadFields();
if (LineData == null)
{
break;
}
dt.Rows.Add(new object[] { LineData[0], LineData[1] });
}
}

Requires Microsoft.VisualBasic reference




Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


No comments:

Post a Comment