Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用C编写CSV解析器#_C#_Csv_Parsing - Fatal编程技术网

C# 用C编写CSV解析器#

C# 用C编写CSV解析器#,c#,csv,parsing,C#,Csv,Parsing,我最近开始了一个项目的工作,该项目搜索CSV文件中的重复条目,并向用户提供删除一个或两个条目的选项 看起来很简单,但是我对将CSV文件解析到内存中的函数有一个问题 这是有问题的代码 using System; using System.IO; using Microsoft.VisualBasic.FileIO; using System.Collections.Generic; using System.Windows.Forms; public List<string[]>

我最近开始了一个项目的工作,该项目搜索CSV文件中的重复条目,并向用户提供删除一个或两个条目的选项

看起来很简单,但是我对将CSV文件解析到内存中的函数有一个问题

这是有问题的代码

using System;
using System.IO;
using Microsoft.VisualBasic.FileIO;
using System.Collections.Generic;
using System.Windows.Forms;


public List<string[]> parseCSV(string path)
{
    List<string[]> parsedData = new List<string[]>();
    string[] fields;

    TextFieldParser parser = null;
    string line = parser.ReadLine();

    try
    {
        /*TextFieldParser*/ parser = new TextFieldParser(@"c:\temp\test.csv");
        parser.TextFieldType = FieldType.Delimited;
        parser.SetDelimiters(",");

        while (!parser.EndOfData)
        {
            fields = parser.ReadFields();
            parsedData.Add(fields);

            //Did more stuff here with each field.
        }

        parser.Close();

    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message);
    }

   return parsedData;
}
使用系统;
使用System.IO;
使用Microsoft.VisualBasic.FileIO;
使用System.Collections.Generic;
使用System.Windows.Forms;
公共列表解析CSV(字符串路径)
{
List parsedData=新列表();
字符串[]字段;
TextFieldParser=null;
string line=parser.ReadLine();
尝试
{
/*TextFieldParser*/parser=newtextfieldparser(@“c:\temp\test.csv”);
parser.TextFieldType=FieldType.Delimited;
parser.SetDelimiters(“,”);
而(!parser.EndOfData)
{
fields=parser.ReadFields();
parsedData.Add(字段);
//每个领域都做了更多的工作。
}
parser.Close();
}
捕获(例外e)
{
MessageBox.Show(e.Message);
}
返回数据;
}
由于某些原因,VS2017中的parseCSV在函数声明中以红色下划线。 我不明白为什么会这样。我尝试了一些明显的修复方法,比如将函数名从parseCSV改为其他名称,但显然没有。在C中,所有内容都包含在一个类中,您不能直接在命名空间中声明方法

using System;
using System.IO;
using Microsoft.VisualBasic.FileIO;
using System.Collections.Generic;
using System.Windows.Forms;

class MyLearningOnlyCsvParser {

  public List<Customer_Data> parseCSV(string path)
  {
    List<Customer_Data> parsedData = new List<Customer_Data>();
    string[] fields;

    TextFieldParser parser = null;
    string line = parser.ReadLine();

    try
    {
        /*TextFieldParser*/ parser = new TextFieldParser(@"c:\temp\test.csv");
        parser.TextFieldType = FieldType.Delimited;
        parser.SetDelimiters(",");

        while (!parser.EndOfData)
        {
            fields = parser.ReadFields();

            // assume the CSV is always with  11 columns
            if(fields.length == 11) {
                Customer_Data newData = new Customer_Data();
                newData.name = fields[0];
                newData.company = fields[1];
                // assign to the rest of the customer data properties with each fields
                parsedData.Add(newData);
            }
            else {
                // error handling of not well formed CSV
            }

        }

        parser.Close();

    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message);
    }

   return parsedData;
  }
}
使用系统;
使用System.IO;
使用Microsoft.VisualBasic.FileIO;
使用System.Collections.Generic;
使用System.Windows.Forms;
类MyLearningOnlyCsvParser{
公共列表解析CSV(字符串路径)
{
List parsedData=新列表();
字符串[]字段;
TextFieldParser=null;
string line=parser.ReadLine();
尝试
{
/*TextFieldParser*/parser=newtextfieldparser(@“c:\temp\test.csv”);
parser.TextFieldType=FieldType.Delimited;
parser.SetDelimiters(“,”);
而(!parser.EndOfData)
{
fields=parser.ReadFields();
//假设CSV始终包含11列
如果(fields.length==11){
客户_数据newData=新客户_数据();
newData.name=字段[0];
newData.company=字段[1];
//将每个字段分配给其他客户数据属性
parsedData.Add(newData);
}
否则{
//格式不正确的CSV的错误处理
}
}
parser.Close();
}
捕获(例外e)
{
MessageBox.Show(e.Message);
}
返回数据;
}
}

错误是什么?我没有看到红色下划线,只是对命名约定的抱怨。如果将鼠标悬停在曲线上,您可以看到错误。这是怎么一回事?我们不太善于猜测那样的事情。它只是想让您将
ParseCSV
重命名为
ParseCSV
?如果是,则忽略它(后者是大多数约定的首选名称)。您是否收到任何错误或重要警告?您应该知道,这一切都是以前做过的:有大量的CSV解析器,它们做了非常棒的事情(比VB做的更好)。请参阅CSVHelper以获取实例当我将光标悬停在函数名上时,我会收到一个错误消息:“List.ParseCSV(字符串路径)命名空间不能直接包含字段或方法等成员”。至于不满者的评论。我知道有一百万个CSV解析器比我的要好得多,但正如我之前所说,这主要是一个学习项目。如果这是我在专业环境下写的东西,我绝对会使用更好的选择。好的,谢谢你的提示。从历史上看,我主要是用C++工作的,所以有些是我新的。