我有一个类(由filehelpers使用),当我尝试定义一个可空字符串时,它给出了一个错误:
public String? ItemNum;
Run Code Online (Sandbox Code Playgroud)
错误是:
Error 1 The type 'string' must be a non-nullable value type in order
to use it as parameter 'T' in the generic type or method 'System.Nullable<T>'
Run Code Online (Sandbox Code Playgroud)
即使使用小写字母也会出现这种情况string,尽管我还没有看到它们之间的区别.
使用其他类型,如int,decimal等是好的:
public decimal? ItemNum;
Run Code Online (Sandbox Code Playgroud)
一些关于网络的一般性讨论关于按字段等定义构造函数,但鉴于其他字段工作正常,字符串有什么特别之处?有一种优雅的方法可以避免它吗?
我正在尝试使用FileHelpers(http://www.filehelpers.net/)解析一个非常大的csv文件.该文件为1GB压缩文件,解压缩约20GB.
string fileName = @"c:\myfile.csv.gz";
using (var fileStream = File.OpenRead(fileName))
{
using (GZipStream gzipStream = new GZipStream(fileStream, CompressionMode.Decompress, false))
{
using (TextReader textReader = new StreamReader(gzipStream))
{
var engine = new FileHelperEngine<CSVItem>();
CSVItem[] items = engine.ReadStream(textReader);
}
}
}
Run Code Online (Sandbox Code Playgroud)
FileHelpers然后抛出OutOfMemoryException.
测试失败:抛出了类型'System.OutOfMemoryException'的异常.System.OutOfMemoryException:抛出了类型'System.OutOfMemoryException'的异常.System.Text.StringBuilder.ExpandByABlock(Int32 minBlockCharCount)在System.Text.StringBuilder.Append(Char值,Int32 repeatCount)的System.Text.StringBuilder.Append(Char值)处于FileHelpers.StringHelper.ExtractQuotedString(LineInfo line,Char在FileHelpers.FileHelperEngine的FileHelpers.RecordInfo.StringToRecord(LineInfo行)的FileHelpers.FieldBase.ExtractValue(LineInfo行)的FileHelpers.DelimitedField.ExtractFieldString(LineInfo行)处的quoteChar,Boolean allowMultiline)
1.ReadStream(TextReader reader, Int32 maxRecords, DataTable dt) at FileHelpers.FileHelperEngine.ReadStream(TextReader reader)
是否可以使用FileHelpers解析这么大的文件?如果没有,任何人都可以推荐一种解析文件的方法吗?谢谢.
我已成功将CSV文件拖到以下类中:
[DelimitedRecord(",")]
[IgnoreFirst(1)] // ignores first line of file, since it's a header
public class Employee {
public string EmployeeId;
public string FirstName;
public string LastName;
// etc.
}
Run Code Online (Sandbox Code Playgroud)
我需要基于该类创建一个DataTable才能使用SqlBulkCopy.我找到了几个例子,但以下方法对我不起作用:
private static DataTable createEmptyDataTable(Type myType) {
DataTable dt = new DataTable();
foreach (PropertyInfo info in myType.GetProperties()) {
dt.Columns.Add(new DataColumn(info.Name, info.PropertyType));
}
return dt;
}
Run Code Online (Sandbox Code Playgroud)
问题出在myType.GetProperties()上.它没有抛出错误,但它什么也没有返回.它应该返回的PropertyInfo数组是空的.已经有一段时间了,无法弄清楚问题......
编辑:我也使用这个变种没有成功:
private static DataTable createEmptyDataTable(Type myType) {
DataTable dt = new DataTable();
PropertyInfo[] infoArray = myType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
foreach (PropertyInfo info …Run Code Online (Sandbox Code Playgroud) PresentationFramework.dll中出现类型'System.Windows.Markup.XamlParseException'的第一次机会异常
附加信息:'对类型'filehelpertest.MainWindow'的构造函数的调用与指定的绑定约束相匹配引发异常.行号"3"和行位置"9".
大家好,
我是FileHelpers的新手.
我在VS Express 2013中制作了一个最小的WPF项目,以便解决这个问题.代码将从FileHelpers文档中的"Quick Start for Delimited files"部分复制.
我试过引用3个不同版本的FileHelpers.dll(2.0,1.1,Mono1.2),我尝试重启.但是我看不到任何影响.必须有一些非常简单的东西,我错过了吗?
或者FileHelpers不适用于较新版本的.NET?
谢谢!
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using FileHelpers;
namespace filehelpertest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
FileHelperEngine engine = new FileHelperEngine(typeof(Customer));
// To Read Use:
Customer[] res = engine.ReadFile("f1.txt") as Customer[];
// To Write Use:
engine.WriteFile("f2.txt", res);
}
[DelimitedRecord(",")]
public class Customer
{
public int CustId;
public …Run Code Online (Sandbox Code Playgroud) 在这里,我必须使用FileHelpers和C#编写一个记录,其中记录用管道分隔。大部分字段的长度是可变的(因此,我的记录将是[DelimitedRecord(“ |”)])。但是某些字段必须具有固定的长度(它们必须具有填充,特定格式等)。
我用谷歌搜索了一堆,没有目标如何实现。
例:
[DelimitedRecord("|")]
public class Customer
{
public int CustId; //variable length
public string Name; //variable length
public decimal Balance; //variable length
[FieldConverter(ConverterKind.Date, "dd-MM-yyyy")]
public DateTime AddedDate;
public int Code; // this one must have 10 characters with "zero-fill", like
// 153 must look like 0000000153
}
Run Code Online (Sandbox Code Playgroud)
我该怎么做?我是否必须使用Converter方法并为此编写自己的Converter?
先感谢您。
示例代码:
using System.Collections.Generic;
using FileHelpers;
....
private void Save<T>(string destFilename, IEnumerable<T> data) where T : class
{
var engine = new FileHelperEngine((typeof(T)));
engine.HeaderText = engine.GetFileHeader();
engine.WriteFile(destFilename, data); // XX
}
Run Code Online (Sandbox Code Playgroud)
在第XX行,engine.WriteFile的第二个参数是期望IEnumerable <object>.这段代码工作正常.
我的问题是,为什么方法需要"where T:class"约束?如果我删除它,我得到编译时错误:
Argument 2: cannot convert from
'System.Collections.Generic.IEnumerable<T>' to
'System.Collections.Generic.IEnumerable<object>'
Run Code Online (Sandbox Code Playgroud)
我原以为一切都是"对象",所以约束不是必要的?
我正在使用 Filehelpers 读取一个简单的 CSV 文件 - 该文件只是一个键值对。(字符串,int64)
我为此编写的 f# 类型是:
type MapVal (key:string, value:int64) =
new()= MapVal("",0L)
member x.Key = key
member x.Value = value
Run Code Online (Sandbox Code Playgroud)
我在这里遗漏了一些基本的东西,但 FileHelpers 总是假设字段的顺序与我指定的顺序相反 - 如值、键。
let dfe = new DelimitedFileEngine(typeof<MapVal>)
let recs = dfe.ReadFile(@"D:\e.dat")
recs |> Seq.length
Run Code Online (Sandbox Code Playgroud)
我在这里缺少什么?
今天早上,我开始尝试使用自定义字段属性的快速练习.尝试了很多东西并搜索了许多例子(大多数涉及类而不是字段属性),我正式陷入困境.
我的代码如下.一个特点是该类是使用classbuilder在FileHelpers中构建的.我的各种部分成功尝试确实设法从这个类获得字段名,所以我相信这部分工作正常.
我想做的(根据代码中的注释)是a)遍历字段,b)为每个字段运行,查看DBDataTypeAttribute属性是否存在,以及c)看似最难的部分 - 从属性中获取值(FieldType字符串) ,和AllowNulls布尔).
任何评论赞赏!
标记
class Program
{
static void Main(string[] args)
{
// Desired output:
System.Type userType = null;
userType = ClassBuilder.ClassFromString(@"
public class ExpenseReport
{
[FieldQuoted('""', QuoteMode.OptionalForRead, MultilineMode.AllowForRead)]
[DBDataTypeAttribute(FieldType = ""varchar(1000)"", AllowNulls = true)]
public String UniqueID;
[FieldQuoted('""', QuoteMode.OptionalForRead, MultilineMode.AllowForRead)]
public String ERNum;
}");
object[] attributes;
attributes = userType.GetCustomAttributes(typeof(DBDataTypeAttribute), true);
foreach (Object attribute in attributes)
{
// Would like to be able to ID for each field whether the DBDataTypeAttribute is present, and get the …Run Code Online (Sandbox Code Playgroud) 我正在开发 ASP.NET Webforms C# 应用程序。我需要将 CSV 文件上传到服务器,并将内容读取并保存到数据库。我在某处读到FileHelpers可用于读取 csv 文件,但我还没有看到任何处理 HttpPostedFile 的示例。有人有使用 filehelpers 进行文件上传的经验吗?
我也愿意接受替代方法。谢谢。
我正在尝试使用 FileHelpers 从 Azure 文件存储读取 csv 文件。这是如何运作的?
我可以连接到 azure 文件存储并构建对 te 文件的引用。这是通过循环遍历目录和文件来完成的,在本例中,这些被命名为子目录和文件名。
代码:
//connect to storage account
CloudStorageAccount storageAccount = new CloudStorageAccount("link to account and credentials");
//connect to file client
CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
//create cloudfileshare to in process
CloudFileShare inprocessShare = fileClient.GetShareReference("inprocess");
//get reference to root directory of inprocess share
CloudFileDirectory inprocessRootDir = inprocessShare.GetRootDirectoryReference();
//get reference to the subdirectory
CloudFileDirectory inprocessDir = inprocessRootDir.GetDirectoryReference(subdirectory);
//get reference to the current file
CloudFile destfile = inprocessDir.GetFileReference(filename);
Run Code Online (Sandbox Code Playgroud)
这可以获取对我要处理的文件的引用。
当我使用 StreamReader 时,我可以使用以下代码,这可以工作:
\\open stream …Run Code Online (Sandbox Code Playgroud) filehelpers ×10
c# ×9
.net ×4
csv ×3
generics ×2
asp.net ×1
attributes ×1
azure ×1
dotnetnuke ×1
f# ×1
nullable ×1
wpf ×1