diff --git a/DotNetDBF.Enumerable/Enumerable.cs b/DotNetDBF.Enumerable/Enumerable.cs index 8642dd9..4a8b8eb 100644 --- a/DotNetDBF.Enumerable/Enumerable.cs +++ b/DotNetDBF.Enumerable/Enumerable.cs @@ -169,8 +169,9 @@ public static void AddRecord(this DBFWriter writer, Enumerable.IDBFInterceptor v /// /// The reader. /// The prototype. Anonymous class instance + /// Indicates if parsing error throws an exception. Default: true /// - public static IEnumerable AllRecords(this DBFReader reader, T prototype = null) where T : class + public static IEnumerable AllRecords(this DBFReader reader, T prototype = null, bool throwOnParsingError = true) where T : class { var tType = typeof(T); @@ -194,25 +195,25 @@ public static IEnumerable AllRecords(this DBFReader reader, T prototype = if (tType.GetCustomAttributes(typeof(CompilerGeneratedAttribute), false).Any()) { - var tAnon = reader.NextRecord(tProps, tOrderedProps); + var tAnon = reader.NextRecord(tProps, tOrderedProps, throwOnParsingError); while (tAnon != null) { tReturn.Add((T) Activator.CreateInstance(tType, tAnon)); - tAnon = reader.NextRecord(tProps, tOrderedProps); + tAnon = reader.NextRecord(tProps, tOrderedProps, throwOnParsingError); } return tReturn; } - var t = reader.NextRecord(tProps, tOrderedProps); + var t = reader.NextRecord(tProps, tOrderedProps, throwOnParsingError); while (t != null) { var interceptor = new Enumerable.DBFInterceptor(t, tProperties.Select(it => it.Name).ToArray()); tReturn.Add(interceptor.ActLike(typeof(Enumerable.IDBFInterceptor))); - t = reader.NextRecord(tProps, tOrderedProps); + t = reader.NextRecord(tProps, tOrderedProps, throwOnParsingError); } @@ -225,9 +226,10 @@ public static IEnumerable AllRecords(this DBFReader reader, T prototype = /// The reader. /// The where column name. /// What the were column should equal. + /// Indicates if parsing error throws an exception. Default: true /// public static IEnumerable DynamicAllRecords(this DBFReader reader, string whereColumn = null, - dynamic whereColumnEquals = null) + dynamic whereColumnEquals = null, bool throwOnParsingError = true) { var props = reader.GetSelectFields().Select(it => it.Name).ToArray(); @@ -240,7 +242,7 @@ public static IEnumerable DynamicAllRecords(this DBFReader reader, stri var tReturn = new List(); - var t = reader.NextRecord(); + var t = reader.NextRecord(throwOnParsingError); while (t != null) { @@ -249,7 +251,7 @@ public static IEnumerable DynamicAllRecords(this DBFReader reader, stri dynamic tO = t[i]; if (!tO.Equals(whereColumnEquals)) { - t = reader.NextRecord(); + t = reader.NextRecord(throwOnParsingError); continue; } } @@ -259,7 +261,7 @@ public static IEnumerable DynamicAllRecords(this DBFReader reader, stri tReturn.Add(interceptor); - t = reader.NextRecord(); + t = reader.NextRecord(throwOnParsingError); } diff --git a/DotNetDBF/DBFReader.cs b/DotNetDBF/DBFReader.cs index 784b3af..989f355 100644 --- a/DotNetDBF/DBFReader.cs +++ b/DotNetDBF/DBFReader.cs @@ -1,9 +1,9 @@ /* DBFReader Class for reading the records assuming that the given - InputStream comtains DBF data. + InputStream contains DBF data. - This file is part of DotNetDBF packege. + This file is part of DotNetDBF package. original author (javadbf): anil@linuxense.com 2004/03/31 @@ -226,13 +226,12 @@ @returns The next row as an Object array. Types of the elements these arrays follow the convention mentioned in the class description. */ - public object[] NextRecord() + public object[] NextRecord(bool throwOnParsingError = true) { - return NextRecord(_selectFields, _orderedSelectFields); + return NextRecord(_selectFields, _orderedSelectFields, throwOnParsingError); } - - internal object[] NextRecord(IEnumerable selectIndexes, IList sortedIndexes) + internal object[] NextRecord(IEnumerable selectIndexes, IList sortedIndexes, bool throwOnParsingError = true) { if (_isClosed) { @@ -365,8 +364,10 @@ internal object[] NextRecord(IEnumerable selectIndexes, IList sortedIn } catch (FormatException e) { - throw new DBFException("Failed to parse Float", - e); + if (throwOnParsingError) + throw new DBFException("Failed to parse Float", e); + + recordObjects[i] = default(decimal); } break; @@ -399,8 +400,11 @@ internal object[] NextRecord(IEnumerable selectIndexes, IList sortedIn } catch (FormatException e) { - throw new DBFException( - "Failed to parse Number", e); + if (throwOnParsingError) + throw new DBFException( + "Failed to parse Number", e); + + recordObjects[i] = default(decimal); } break;