Domanda

I am using FileHelpers to parse csv file and to load it in a datatable. I am using xml template and pass it to ClassBuilder.LoadFromXml

I want certain characters to be trimmed from a particular field and I think there is something we can mention in Xml file like TrimChars and TrimMode to do this. I tried to do so but it is not working.

for eg.

<DelimitedClass Delimiter=";">
  <ClassName>YourRecordClass</ClassName>
  <SealedClass />
  <IgnoreEmptyLines />  
  <IgnoreFirstLines>1</IgnoreFirstLines>  
  <Fields>   
    <Field Name="MyField" Type="String" TrimChars=";" TrimMode="Right"/> 
  </Fields>
</DelimitedClass>

Thanks for your help in advance.

================ Edit:

I am not getting any error it is just not trimming the semicolon from my last field. Here is the sample, Now in this sample you will see 99999999 ; at the end..I want to trim that semicolon.

HEADER_1   ;HEADER_2;HEADER_3;HEADER_4             ;HEADER_5        ;HEADER_6      ;HEADER_7;HEADER_8   ;HEADER_9;HEADER_10    ;HEADER_11;HEADER_12  ;HEADER_13                      ;HEADER_14;HEADER_15                     ;HEADER_16                  ;HEADER_17               ;HEADER_18;HEADER_19  ;HEADER_20                              ;HEADER_21                                                    ;HEADER_22   ;MyField  ;
000999999;ZZZZZZ_ZZ   ;ZZZZ       ;99zzz9799zzz             ;999Z        ;ZZZZZZZ     ;Z     ;          9;Z   ;ZZZZZZZ;   NOV12;99999999;                  99.990000;    ;                   0.000000;                  -0.990000;                   0.000000;ZZZ      ;ZZZZZZZZ;07/03/2012 12:00:00                    ;07/03/2012 20:35:13                                          ;ZZZ         ;99999999    ;
È stato utile?

Soluzione

Firstly, you are using xml node attributes to specify the field options. Instead, the options should be defined like this:

<Field Name=""MyField"" Type=""String""> 
    <TrimMode>Both</TrimMode>
</Field>

Secondly, the TrimChars attribute will not work when it's the same as the field delimiter. Instead you should add an additional dummy optional final field. Like this:

<Fields>   
    <Field Name=""MyField"" Type=""String""> 
        <TrimMode>Both</TrimMode>
    </Field>
    <Field Name=""IgnoreTrailingDelimiter"" Type=""String""> 
        <FieldOptional />
    </Field>
</Fields>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top