Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.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# 如何在导入CSV文件行之前验证它们_C#_.net_Asp.net Mvc_Entity Framework - Fatal编程技术网

C# 如何在导入CSV文件行之前验证它们

C# 如何在导入CSV文件行之前验证它们,c#,.net,asp.net-mvc,entity-framework,C#,.net,Asp.net Mvc,Entity Framework,我有以下平面文件: 如您所见,有一行显然格式不正确 我有以下代码: public class FlatFileModel { public string Key { get; set; } public string ArticleNumber { get; set; } public string ColorCode { get; set; } public string Description { get; set;

我有以下平面文件:

如您所见,有一行显然格式不正确

我有以下代码:

 public class FlatFileModel
    {
        public string Key { get; set; }
        public string ArticleNumber { get; set; }
        public string ColorCode { get; set; }
        public string Description { get; set; }
        public string Price { get; set; }
        public string ActionPrice { get; set; }
        public string Delivery { get; set; }
        public string Q1 { get; set; }
        public string Size { get; set; }
        public string Color { get; set; }
    }




[HttpPost]
        public ActionResult Upload()
        {
            if (Request.Files.Count > 0)
            {
                var httpPostedFileBase = Request.Files[0];
                if (httpPostedFileBase != null && httpPostedFileBase.ContentLength > 0)
                {
                    //read data from input stream
                    using (var csvReader = new System.IO.StreamReader(httpPostedFileBase.InputStream))
                    {
                        string inputLine = "";

                        List<FlatFileModel> lineaArchivo = new  List<FlatFileModel>();
                        //read each line
                        while ((inputLine = csvReader.ReadLine()) != null)
                        {
                            //get lines values
                            string[] values = inputLine.Split(new char[] { ',' });

                            for (int x = 0; x < values.Length; x++)
                            {
                                lineaArchivo.Add(new FlatFileModel()
                                {
                                    Key = values[0],
                                    ArticleNumber = values[1],
                                    ColorCode = values[2],
                                    Description = values[3],
                                    Price = values[4],
                                    ActionPrice = values[5],
                                    Delivery = values[6],
                                    Q1 = values[7],
                                    Size = values[8],
                                    Color = values[9]
                                });
                            }
                        }

                        csvReader.Close();
                    }
                }
                //var file = Request.Files[0];

                //if (file != null && file.ContentLength > 0)
                //{
                //    var fileName = Path.GetFileName(file.FileName);
                //    var path = Path.Combine(Server.MapPath("~/CSVs/"), fileName);
                //    file.SaveAs(path);
                //}
            }

            return RedirectToAction("Index");
        }
公共类FlatFileModel
{
公共字符串密钥{get;set;}
公共字符串ArticleNumber{get;set;}
公共字符串颜色代码{get;set;}
公共字符串说明{get;set;}
公共字符串Price{get;set;}
公共字符串ActionPrice{get;set;}
公共字符串传递{get;set;}
公共字符串Q1{get;set;}
公共字符串大小{get;set;}
公共字符串颜色{get;set;}
}
[HttpPost]
公共行动结果上载()
{
如果(Request.Files.Count>0)
{
var httpPostedFileBase=Request.Files[0];
if(httpPostedFileBase!=null&&httpPostedFileBase.ContentLength>0)
{
//从输入流读取数据
使用(var csvReader=new System.IO.StreamReader(httpPostedFileBase.InputStream))
{
字符串inputLine=“”;
List LINEArchivo=新列表();
//读每一行
而((inputLine=csvReader.ReadLine())!=null)
{
//获取行值
字符串[]值=inputLine.Split(新字符[]{',});
对于(int x=0;x0)
//{
//var fileName=Path.GetFileName(file.fileName);
//var path=path.Combine(Server.MapPath(“~/CSVs/”),文件名);
//file.SaveAs(路径);
//}
}
返回操作(“索引”);
}
我需要一种简单的方法来检测不符合格式的行,所以我可以忽略它们,或者将它们记录为错误,但我不确定如何才能做到这一点


谢谢

因为所有内容都是一个字符串,您只需检查您实际从中读取的行中的元素数是否正确即可

if (values.Length == 10) {
    // row ok, do your for-loop
}

由于所有内容都是一个字符串,您只需检查实际从中读取的行中的元素数是否正确即可

if (values.Length == 10) {
    // row ok, do your for-loop
}

关于如何验证数据,一个好的方法是考虑什么构成有效行与无效行,然后编纂这些规则。

然后,您可以使用它来构建一个regex语句,用于标识不符合该模式的行


例如,您可以检查每个字段是否匹配适当的模式(每行的第2列应包含一个整数,第5列应包含一个2位十进制值,第6列应不包含任何内容,第9列应包含一个整数,等等)。

关于如何验证数据,这样做的一个好方法是考虑什么构成有效行与无效行,然后编纂这些规则。

然后,您可以使用它来构建一个regex语句,用于标识不符合该模式的行


例如,您可以检查每个字段是否匹配适当的模式(每行的第2列应包含一个整数,第5列应包含一个2位十进制值,第6列应不包含任何内容,第9列应包含一个整数,等等)。

对于您的情况,尤其是
字符串[]中的元素数值
将不是8。你为什么不把这一点具体地说出来呢,
string[]values
中的元素数不是8。为什么你不把它说得足够简单,对于一个简单的测试来说应该可以,对于现实生活,我需要像STLDeveloper建议的那样做更复杂的校准,对于一个简单的测试来说应该可以,对于现实生活,我需要像STLDeveloper建议的那样做更复杂的校准