コード例 #1
0
        //-> Name:Ignore Mode Error handling
        //-> Description:Read the file dropping bad records
        //-> File:RunEngine.cs
        /// <summary>
        /// Run an example of running a file with an error using the
        /// IgnoreMode option to silently drop bad records
        /// </summary>
        public override void Run()
        {
            var engine = new DelimitedFileEngine<Customer>();

            // Switch error mode on
            engine.ErrorManager.ErrorMode = ErrorMode.IgnoreAndContinue;

            //  This fails with not in enumeration error
            Customer[] customers = engine.ReadFile("Input.txt");

            // This wont display anything, we have dropped it
            foreach (var err in engine.ErrorManager.Errors)
            {
                Console.WriteLine();
                Console.WriteLine("Error on Line number: {0}", err.LineNumber);
                Console.WriteLine("Record causing the problem: {0}", err.RecordString);
                Console.WriteLine("Complete exception information: {0}", err.ExceptionInfo.ToString());
            }

            // This will display only 3 of the four records
            foreach (var cust in customers)
            {
                Console.WriteLine("Customer name {0} is a {1}", cust.ContactName, cust.ContactTitle);
            }
        }
コード例 #2
0
        //-> /File
        //-> File:RunEngine.cs
        public override void Run()
        {
            var engine = new DelimitedFileEngine<Customer>();

            //  Read input records, enumeration automatically converted
            Customer[] customers = engine.ReadFile("Input.txt");

            foreach (var cust in customers)
                Console.WriteLine("Customer name {0} is a {1}", cust.ContactName, cust.ContactTitle);
        }
コード例 #3
0
        //-> Name:ErrorMode saving Errors
        //-> Description:Read the file saving bad records
        //-> File:RunEngine.cs
        /// <summary>
        /// Run an example of running a file with an error using the
        /// ErrorMode option to capture bad records and then saving them
        /// </summary>
        public override void Run()
        {
            var engine = new DelimitedFileEngine<Customer>();

            // Switch error mode on
            engine.ErrorManager.ErrorMode = ErrorMode.SaveAndContinue;

            //  This fails with not in enumeration error
            Customer[] customers = engine.ReadFile("Input.txt");

            if (engine.ErrorManager.HasErrors)
                engine.ErrorManager.SaveErrors("errors.out");
            LoadErrors();
        }
コード例 #4
0
        //-> /File

        public override void Run()
        {
            //-> File:Example.txt

            var engine = new DelimitedFileEngine<CustomersVerticalBar>();

            engine.Options.Fields[2].TrimMode = TrimMode.Both;
            engine.Options.RemoveField("DummyField");
            
            // City is optional
            engine.Options.Fields[engine.Options.Fields.Count - 1].IsOptional = true;

            engine.ReadFile("Input.txt");

            //-> /File

        }
コード例 #5
0
        //-> Name:ErrorMode Error handling
        //-> Description:Read the file rejecting bad records
        //-> File:RunEngine.cs
        /// <summary>
        /// Run an example of running a file with an error using the
        /// ErrorMode option to capture bad records
        /// </summary>
        /// <remarks>
        /// In the standard mode you can catch the exceptions when something fails.
        /// </remarks>
        public override void Run()
        {
            var engine = new DelimitedFileEngine<Customer>();

            // Switch error mode on
            engine.ErrorManager.ErrorMode = ErrorMode.SaveAndContinue;

            //  Only record that fails will not be present
            Customer[] customers = engine.ReadFile("Input.txt");

            // This will display error from line 2 of the file.
            foreach (var err in engine.ErrorManager.Errors) {
                Console.WriteLine();
                Console.WriteLine("Error on Line number: {0}", err.LineNumber);
                Console.WriteLine("Record causing the problem: {0}", err.RecordString);
                Console.WriteLine("Complete exception information: {0}", err.ExceptionInfo.ToString());
            }
        }
コード例 #6
0
        //-> /File

        public override void Run()
        {
            //-> File:Example.cs
            try
            {
                var engine = new DelimitedFileEngine<Customer>();
                
                //  This fails with not in enumeration error
                var customers = engine.ReadFile("Input.txt");
                
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString()); // with stack trace
            }
            //-> /File

        }
コード例 #7
0
        //-> Name:Simple Error handling
        //-> Description:Read the file or reject the whole file
        //-> File:RunEngine.cs
        /// <summary>
        /// Run an example of running a file with an error through and
        /// using a try catch to collect the error.
        /// </summary>
        /// <remarks>
        /// In the standard mode you can catch the exceptions when something fails.
        /// </remarks>
        public override void Run()
        {
            try {
                var engine = new DelimitedFileEngine<Customer>();

                //  This fails with not in enumeration error
                Customer[] customers = engine.ReadFile("Input.txt");

                // this will not happen because of the exception
                foreach (var cust in customers) {
                    Console.WriteLine("Customer name {0} is a {1}",
                        cust.ContactName,
                        cust.ContactTitle);
                }
            }
            catch (Exception ex) {
                // Console.WriteLine(ex.ToString()); // with stack trace
                this.Console.WriteLine(ex.Message);
            }
        }
コード例 #8
0
ファイル: API.cs プロジェクト: cbinding/stellar
        /// <summary>Read a delimited data file to a DataTable</summary>
        /// <param name="fileName">The name of the delimited file to read, including absolute or relative path as appropriate</param>
        /// <param name="delimiter">Delimiter character to be used</param>
        /// <param name="hasHeader">Is there a header row containing column names? If not they will be generated automatically</param>
        /// <param name="maxRows">Only read this many rows (useful for testing with larger files)</param>
        /// <returns>The number of records processed</returns>
        /// <exception cref="System.ArgumentException">Throws an exception if delimited file name is not supplied</exception>
        /// <exception cref="System.Exception">Throws an exception if delimited file name does not exist or cannot be accessed</exception>
        public static DataTable Delimited2DT(string fileName, char delimiter, bool hasHeader, int maxRows)
        {
            //Fail if fileName not passed in
            if (fileName == String.Empty)
                throw new ArgumentException("file name required", "fileName");

            //Set up the delimited file reader
            DelimitedFileEngine<DelimitedRow> engine = new DelimitedFileEngine<DelimitedRow>();
            engine.Options.Delimiter = delimiter.ToString();
            engine.Options.IgnoreEmptyLines = true;
            //engine.Options.IgnoreCommentedLines = true;

            DataTable dt = new DataTable();
            long recordCount = 0;
            DelimitedRow[] rows;

            if (!System.IO.File.Exists(fileName))
            {
                throw new Exception(string.Format("Problem finding file {0}?", fileName));
            }
            try
            {
                rows = engine.ReadFile(fileName, maxRows);
                //TODO: Need to allow reading a file that is already open elsewhere
                //System.IO.TextReader tr1 = new System.IO.StreamReader(cmdFileName);
                //System.IO.TextReader tr2 = System.IO.File.OpenText(cmdFileName);
                //rows = engine.ReadStream(tr,maxRows);
                //tr.Close();
            }
            catch (System.Exception ex)
            {
                throw new Exception(string.Format("Problem reading file {0}? Check the file exists and can be accessed, and is not currently open in another application", fileName), ex);
            }

            foreach (DelimitedRow row in rows)
            {
                //Is this the first row?
                if (recordCount == 0)
                {
                    if (hasHeader)
                    {
                        // First row contains column names
                        foreach (String s in row.fieldValues)
                        {
                            if (s != null && s.Trim().Length > 0 && !dt.Columns.Contains(s))
                                dt.Columns.Add(getValidColumnName(s), typeof(String));
                            else
                                dt.Columns.Add(getNextColumnName(dt), typeof(String));
                        }
                    }
                    else
                    {
                        // First row just contains values so create default column names for each field
                        //TODO: there's a CHANCE that first row may not have as many values as there are columns
                        //not sure how to guard against that yet...
                        foreach (String s in row.fieldValues)
                        {
                            dt.Columns.Add(getNextColumnName(dt), typeof(String));
                        }
                        dt.Rows.Add(row.fieldValues);
                    }
                }
                else //not first row
                {
                    //still a CHANCE that we have more or less field values than DataTable columns
                    //Ensure that what's being added will match up with DataTable.Columns.Count, however
                    //this may hide errors where a field value contains commas and is incorrectly parsed
                    String[] fieldValues = new String[dt.Columns.Count];

                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        if (i < row.fieldValues.Length)
                            fieldValues[i] = row.fieldValues[i];
                    }
                    //fieldValues.Length should now match DataTable.Columns.Count

                    //Strip leading and trailing quotes if present
                    //(CSV engine should be doing this but doesn't seem to)
                    for (int i = 0; i < fieldValues.Length; i++)
                    {
                        if ((fieldValues[i] != null) &&
                            (fieldValues[i].StartsWith("\"") &&
                            (fieldValues[i].EndsWith("\""))))
                            fieldValues[i] = fieldValues[i].Substring(1, fieldValues[i].Length - 2);

                        //05/09/12 - UNICODE?
                        //if (!fieldValues[i].IsNormalized())
                            //fieldValues[i] = EscapeUnicode(fieldValues[i]);

                    }

                    dt.Rows.Add(fieldValues);
                }
                recordCount++;
                if (maxRows >= 0 && recordCount >= maxRows)
                    break;
            }
            return dt;
        }
コード例 #9
0
 public static IEnumerable<IQuote> ReadFile(this string filePath)
 {
     var fileEngine = new DelimitedFileEngine<QuoteImport>();
     return fileEngine.ReadFile(filePath);
 }