Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.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# - Fatal编程技术网

C# 处理字段不一致的文本文件

C# 处理字段不一致的文本文件,c#,C#,供应商提供了一个带分隔符的文本文件,但该文件可以而且很可能会为每个客户定制。因此,如果规范提供100个字段,我可能只收到10个字段 我关心的是每个循环的开销。总之,我使用了一个while和两个for循环,只用于标题,而对于细节,至少会有同样多的循环 我的答覆如下: using (StreamReader sr = new StreamReader(flName)) { //Process first line to get field na

供应商提供了一个带分隔符的文本文件,但该文件可以而且很可能会为每个客户定制。因此,如果规范提供100个字段,我可能只收到10个字段

我关心的是每个循环的开销。总之,我使用了一个while和两个for循环,只用于标题,而对于细节,至少会有同样多的循环

我的答覆如下:

        using (StreamReader sr = new StreamReader(flName))
        {
            //Process first line to get field names
            flHeader = sr.ReadLine().Split(charDelimiters);

            //Check first field to determine header or detail file
            if (flHeader[0].ToUpper() == "ORDERID")
            {
                header = true;
            } else if (flHeader[0].ToUpper() == "ORDERITEMID"){
                detail = true;
            }
        }

        //Use TextFieldParser to read and parse files
        using (TextFieldParser parser = new TextFieldParser(flName))
        {
            parser.TextFieldType = FieldType.Delimited;
            parser.SetDelimiters(delimiters);

            while (!parser.EndOfData)
            {
                string[] fields = parser.ReadFields();
                //Send read line to header or detail processor
                if (header == true)
                {
                    if (flHeader[0] != fields[0])
                    {
                        ProcessHeader(fields);
                    }
                }

                if (detail == true)
                {
                    if (flHeader[0] != fields[0])
                    {
                        ProcessDetail(fields);
                    }
                }
            }
//头处理器代码段

        //Declare header class
        Data.BLL.OrderExportHeader_BLL OrderHeaderBLL = new Data.BLL.OrderExportHeader_BLL();
        foreach (string field in fields)
        {
            int fldCnt = fields.Count();
            //Loop through each field then use the switch to determine which field is to be filled in
            for (int flds = 0; flds < fldCnt; flds++ )
            {
                string strField = field.Trim();
                switch (flHeader[flds].ToUpper())
                {
                    case "ORDERID":
                        OrderHeaderBLL.OrderID = strField;
                        break;
                 }
             }
          }

您的示例数据是关键,您的示例目前还不清楚,但我认为它与下面的描述相匹配

根据您的示例,100个字段中有10个字段

在分析每一行时,您只需要将其拆分为10个字段。看起来您是由空格分隔的,但您有一个问题,即字段可能包含嵌入的空格。也许您的数据实际上是以制表符分隔的,在这种情况下您就可以了

为简单起见,我假设您的100个字段的名称为“fld0”、“fld1”、“fld99”

现在,假设接收到的文件包含此头文件

fld10、fld50、fld0、fld20、fld80、fld70、fld0、fld90、fld50、fld60

一行数据看起来像

阿尔法布拉沃查理三角洲回声狐步高尔夫酒店印度朱丽叶

e、 g

拆分[0]=“Alpha”,拆分[1]=“Bravo”等

您分析标题,发现100个字段的主列表中的索引是10、50、0等等。 因此,使用这些索引值构建一个lookupFld数组,即lookupFld[0]=10,lookupFld[1]=50,等等

现在,当您处理每一行时,将其拆分为10个字段,并立即对主字段列表中正确的对应字段进行索引查找

现在主列表[0]=“fld0”、主列表[1]=“fld1”、…、主列表[99]=“fld99”


对于(ii=0;ii)您可以提供一些示例文件?您是指示例文本文件吗?是的,这就是我的意思。当我发布时,它将数据搞砸了,但标题已经发布了。提供的文本文件示例中的空格是随机的吗?它应该与供应商提供的完全相同,因为解析“ShopZio True”一个空格分隔符和“465洋脊道“一个空格分隔符。数据以制表符分隔而不是固定宽度。这就是我现在正在做的。我拆分第一行,即标题。然后在分析每行时,我在switch语句中验证拆分的标题,以查看提供了哪个字段。然后我将字段的数据写入相应的类getter/setter中。
OrderID ManufacturerID  CustomerID  SalesRepID  PONumber    OrderDate   CustomerName    CustomerNumber  RepNumber   Discount    Terms   ShipVia Notes   ShipToCompanyName   ShipToContactName   ShipToContactPhone  ShipToFax   ShipToContactEmail  ShipToAddress1  ShipToAddress2  ShipToCity  ShipToState ShipToZip   ShipToCountry   ShipDate    BillingAddress1 BillingAddress2 BillingCity BillingState    BillingZip  BillingCountry  FreightTerm PriceLevel  OrderType   OrderStatus IsPlaced    ContactName ContactPhone    ContactEmail    ContactFax  Exported    ExportDate  Source  ContainerName   ContainerCubes  Origin  MarketName  FOB SubTotal    OrderTotal  TaxRate TaxTotal    ShippingTotal   IsDeleted   IsContainer OrderGUID   CancelDate  DoNotShipBefore WrittenByName   WrittenForName  WrittenForRepNumber CatalogCode CatalogName ShipToCode
491975  18  0   2621    1234    7/17/2014   RepZio  2499174     0           Test            561-351-7416        max@repzio.com  465 Ocean Ridge Way     Juno Beach  FL  33408       7/18/2014   465 Ocean Ridge Way     Juno Beach  FL  33408   USA     0       ShopZio True    Max Fraser  561-351-7416    max@repzio.com      False       ShopZio     0.00        ShopZio     1500.0000   1500.0000   0.000   0.0000  0.0000  False   False   63960a7b-86b7-47a2-ad11-9763a6b52fd0    7/31/2014   7/18/2014                       
for (ii=0; ii<lookupFld.count; ++ii)
{
    // MasterField[lookupFld[ii]] is represented by with split[ii]

    // when ii = 0
    // lookupFld[0] is 10 
    // so MasterField[10] /* fld10 */ is represented by split[0] /* alpha */
}