1

大家好,我需要解析一些文件以加载到 DataSet 中,但我遇到了第一行值有时为空白的问题,因此当我解析数据时,添加到列中的行已关闭,因为该行没有值[路线代码]。

示例 数据列位于第一行(制表符分隔) 数据行位于以下行(制表符分隔)
RouteCode City EmailAddress FirstName
NULL MyCity My-Email 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;
    }
}
}
4

1 回答 1

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());
}
于 2013-03-04T23:28:25.933 回答