2013-03-04 40 views
1

大家好我有一个解析一些文件加载​​到数据集,我遇到了一个问题,第一行的值有时是空的,所以当我解析数据添加到列的行已关闭,因为行[RouteCode]没有值。解析制表符分隔的文件检测第一行的值是否为空/选项卡

示例数据 列在第一行(制表符分隔)的DataRows在以下行(制表符分隔)
RouteCode市EmailAddress的名字
NULL MyCity我的电子邮件MyFirstName

我所看到的是所有的列都添加好,但每行添加第一个标签值没有检测到,所以它移动列(希望我是有道理的),所以在这种情况下,城市数据坐在RouteCode列,最后一列以某种方式获取第一行值(标签)。

class TextToDataSet 
{ 
    public TextToDataSet() 
    { } 

    /// <summary> 
    /// Converts a given delimited file into a dataset. 
    /// Assumes that the first line  
    /// of the text file contains the column names. 
    /// </summary> 
    /// <param name="File">The name of the file to open</param>  
    /// <param name="TableName">The name of the 
    /// Table to be made within the DataSet returned</param> 
    /// <param name="delimiter">The string to delimit by</param> 
    /// <returns></returns> 
    public static DataSet Convert(string File, 
    string TableName, string delimiter) 
    { 
     //The DataSet to Return 
     DataSet result = new DataSet(); 

     //Open the file in a stream reader. 
     using (StreamReader s = new StreamReader(File)) 
     { 
      //Split the first line into the columns  
      string[] columns = s.ReadLine().Split(delimiter.ToCharArray()); 
      //Add the new DataTable to the RecordSet 
      result.Tables.Add(TableName); 
      //Cycle the colums, adding those that don't exist yet 
      //and sequencing the one that do. 
      foreach (string col in columns) 
      { 
       bool added = false; 
       string next = ""; 
       int i = 0; 
       while (!added) 
       { 
        //Build the column name and remove any unwanted characters. 
        string columnname = col + next; 
        columnname = columnname.Replace("#", ""); 
        columnname = columnname.Replace("'", ""); 
        columnname = columnname.Replace("&", ""); 
        //See if the column already exists 
        if (!result.Tables[TableName].Columns.Contains(columnname)) 
        { 
         //if it doesn't then we add it here and mark it as added 
         result.Tables[TableName].Columns.Add(columnname); 
         added = true; 
        } 
        else 
        { 
         //if it did exist then we increment the sequencer and try again. 
         i++; 
         next = "_" + i; 
        } 
       } 
      } 
      //Read the rest of the data in the file.   
      string AllData = s.ReadToEnd(); 
      //Split off each row at the Carriage Return/Line Feed 
      //Default line ending in most windows exports. 
      //You may have to edit this to match your particular file. 
      //This will work for Excel, Access, etc. default exports. 
      string[] rows = AllData.Split("\n".ToCharArray()); 
      //Now add each row to the DataSet   
      foreach (string r in rows) 
      { 

       //Split the row at the delimiter. 
       string[] items = r.Split(delimiter.ToCharArray()); 
       //Add the item 
       result.Tables[TableName].Rows.Add(items); 
      } 
     } 

     //Return the imported data.   
     return result; 
    } 
} 
} 

回答

0

如果有不应该是任何缺失项文件的任何地方(即存在应始终在标签之间的东西),那么你可以使用:

string[] columns = s.ReadLine().Split(delimiter.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); 

然后检查columns不是一个空数组。如果它然后读取下一行并进行处理:

while (columns.Length == 0) 
{ 
    // Row is empty so read the next line out of the file 
    columns = s.ReadLine().Split(delimiter.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); 
} 

这将确保您的数据总是以填充行开始。但是,如果在列表中进一步列出空白条目,它将会崩溃。

如果有可能是空的条目,那么你可能要检查所有列是空的:

while (columns.All(c => string.IsNullOrEmpty(c))) 
{ 
    // Row is empty so read the next line out of the file 
    columns = s.ReadLine().Split(delimiter.ToCharArray()); 
} 
相关问题