在C#.Net中解析文本文件 [英] parse a text file in C#.Net
问题描述
HI ..我想知道如何解析文本文件.这是文本文件的示例.
HI..i want to know how can i parse a text file..This is the sample of text file.
CompanyName
DepartMentNo:1234
Address:
Date EmpID EmpName Address Department Position
08/08 001 Nazima address of emp Developer Fresher
08/08 002 Vali address of emp Developer Fresher
08/08 003 Farah address of emp DBAdmin Fresher
CompanyName
DepartMentNo:1234
Address:
Date EmpID EmpName Address Department Position
09/08 111 Maryam address of emp HR Fresher
10/08 112 John address of emp TeamLead Seniorlevel
10/08 113 Venkat address of emp DBAdmin Seniorlevel
现在我想要的是基于DepartmantNo和标头(Date,EmpID,EmpName,Address,Department,Position)的信息,我想提取留下不必要的数据和空白行或任何字符的记录. />
Now what i want is based on DepartmantNo and headers(Date,EmpID,EmpName,Address,Department,Position)i want to extract the records leaving the unnecessary data and blank rows or any characters.I am applying logic in this way:
if(DNo!=null && headers.contains(Date)) //taking only column name as reference
{
string str= firstvalue(like 08/08);
Datetime date=Convert.ToDateTime(str);
then same with other values and insert into db.
}
谁能说我如何跳过标题和空白行,然后转到包含实际记录的行.
我已经编写了一些代码来提取DeptNo,但是我现在不知道如何去记录行.
Can anyone say me how can i skip the headers and blankrow and go to the row which contains the actual records.
I have written bit of code for extracting DeptNo but i am unable to figure out how can i go the records row now.
if (MNo!=null && item.Trim().Contains("DTOF"))
{
string[] rec=item.Trim().Split(' ');
for (int i = 0; i < rec.Length; i++)
{
//string Date = rec[rec.Length+1].ToString().Trim();
}
//if(!String.IsNullOrEmpty(item) && item.Length)
}
推荐答案
在代码中执行以下操作以跳过标题和空白行:
Do the following in your code to skip headers and blank rows :
if(item.Length>20 && item.StartsWith("Date")==false)
{
// read your records here
// using split etc.
}
这篇关于在C#.Net中解析文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!