2014-07-20 1 views
0

공급 업체가 구분 된 텍스트 파일을 제공하고 있지만 파일은 각 고객에 대해 사용자 지정 될 수 있습니다. 그래서 스펙에서 100 개의 필드를 제공하는 경우 10 개의 필드 만 수신 할 수 있습니다.필드가 일치하지 않는 텍스트 파일 처리

내 관심사는 각 루프의 오버 헤드입니다. 모두 내가 머리글을위한 루프를 위해 잠시 동안 그리고 2를 사용하고 있으며 세부 사항만큼이나 많을 것입니다.

 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; 
       } 
      } 
      } 

// 헤더 파일 샘플 데이터가 핵심이며,

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  [email protected] 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 [email protected]  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      
+0

"fld99"? –

+0

예제 텍스트 파일을 의미합니까? – jhdeval

+0

네, 그것이 내 뜻입니다. –

답변

0

샘플을 다음과 같이

내 대답은 현재는 모호하지만, 다음 설명과 일치한다고 생각합니다. 각 라인을 구문 분석 가능한 100

중 10 개 필드의 당신의 예를 당

, 당신은 10 개 필드로의 분할해야합니다. 공백으로 구분되는 것처럼 보이지만 필드에 공백이 포함될 수 있다는 점에서 문제가 있습니다. 데이터가 실제 탭으로 구분되어있는 경우에는 괜찮습니다. 편의상

, 나는, 당신의 100 개 필드 이름 'fld0'있다 'FLD1'를 가정 할 것입니다 ... 'fld99'

을 이제 수신 된 파일을 가정하면이 헤더를

fld10을 포함 , fld50, fld0, fld20, fld80, fld70, fld0, fld90, fld50, fld60

및 데이터 라인은

알파 브라보 찰리 델타 에코 폭스 트롯 골프 호텔 인도 줄리엣

같은

를 찾습니다

예 :

분할 [0] = "알파", 분할 [1] = "브라보"등

당신은 헤더를 구문 분석하고 100 개 필드의 마스터 목록에서 인덱스 10,50,0을 것을 발견 그래서 이러한 인덱스 값, 즉 lookupFld [0] = 10, lookupFld [1] = 50 등을 사용하여 lookupFld 배열을 만듭니다.

이제 각 줄을 처리 할 때 10 개의 필드로 나누고 마스터 필드 목록에서 해당 필드의 즉각적인 인덱싱 조회.

이제 MasterList [0] = "fld0", MasterList [1] = "FLD1", ..., MasterList [99] = 당신은 몇 가지 예제 파일을 제공 할 수

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 */ 
} 
+0

데이터는 고정 너비가 아닌 탭으로 구분됩니다. 이것은 내가 지금하고있는 일이다. 헤더 인 첫 번째 줄을 나눕니다. 그런 다음 각 행을 구문 분석 할 때 switch 문에서 split 헤더를 확인하여 제공된 필드를 확인합니다. 그런 다음 해당 필드의 데이터를 해당 클래스 getter/setter에 기록합니다. – jhdeval

관련 문제