@@ -206,6 +206,147 @@ Patterns:
206206 {
207207 Console.WriteLine(t);
208208 }
209+
210+
211+ Batch Parser:<br />
212+ assume we have a txt file with this name "data.txt".<br />
213+ "data.txt":<br />
214+
215+ [alireza,p,0010000000,24,http://github.com/alirezap][ali,pa,0010000230,25,http://nuget.org/alirezap]
216+
217+ first you must create a xml file (syntax) for example:<br />
218+ "syntax.xml":<br />
219+
220+ <?xml version="1.0" encoding="UTF-8"?>
221+ <body>
222+ <syntax>
223+ <record StartWith="[" EndWith="]"/>
224+ </syntax>
225+ <content>
226+ <FIELD1 ID="1" Type="string" Title="name" TERMINATOR=","/>
227+ <FIELD2 ID="2" Type="string" Title="lastname" TERMINATOR=","/>
228+ <FIELD3 ID="3" Type="string" Title="shenasname" TERMINATOR=","/>
229+ <FIELD4 ID="4" Type="int" Title="age" TERMINATOR=","/>
230+ <FIELD5 ID="5" Type="string" Title="url" TERMINATOR="]"/>
231+ </content>
232+ </body>
233+
234+ if records has a boundary you must set that in "StartWith" and "EndWith" attribute in the record element. <br />
235+ in this sample our record is between "[ " and "] ". ([ record] ) --> ([ alireza,p,0010000000,24,http://github.com/alirezap ] ).<br />
236+ so record should be : <br />
237+ <record StartWith =" [ " EndWith =" ] " />. <br />
238+
239+ for next step you must set fields info in syntax file. for example each record in the "data.txt" has 5 section (name,lastname,id,age,url)<br />
240+ so content element should be has 5 field (FIELD1,FIELD2,FIELD3,FIELD4,FIELD5).<br />
241+ each field has 5 property (ID,Type,Title,TERMINATOR,MAX_LENGTH)<br />
242+
243+ ID:<br />
244+ is a unic for field and it must order by data sections.<br />
245+
246+ Type:<br />
247+ is type of field (for example if the first section type in the data.txt is string so Type in the Filed1 must be string).<br />
248+
249+ * Note: supported type: [ string , char , int , double , decimal , byte , bool] <br />
250+
251+ Title:<br />
252+ is a property name for elemnet.( for example first element in the data.txt is name so Title Value in the Filed1 must be name)<br />
253+
254+ * Note: Title Value must be match with your class property name. for example if Title value is Age you must craete a class that have a property with Age name.<br />
255+
256+ TERMINATOR:<br />
257+ specifid delimiter for field. (for example in the "alireza,p,0010000000,24,http://github.com/alirezap " section splited with ',' so TERMINATOR for each fields must be ',')
258+
259+
260+ Full Example:<br />
261+
262+ data.txt:<br />
263+ [ alireza,p,0010000000,24,http://github.com/alirezap ] [ ali,pa,0010000230,25,http://nuget.org/alirezap ]
264+
265+ syntax.xml:<br />
266+
267+ <?xml version="1.0" encoding="UTF-8"?>
268+ <body>
269+ <syntax>
270+ <record StartWith="[" EndWith="]"/>
271+ </syntax>
272+ <content>
273+ <FIELD1 ID="1" Type="string" Title="name" TERMINATOR=","/>
274+ <FIELD2 ID="2" Type="string" Title="lastname" TERMINATOR=","/>
275+ <FIELD3 ID="3" Type="string" Title="shenasname" TERMINATOR=","/>
276+ <FIELD4 ID="4" Type="int" Title="age" TERMINATOR=","/>
277+ <FIELD5 ID="5" Type="string" Title="url" TERMINATOR="]"/>
278+ </content>
279+ </body>
280+
281+ now in the code:<br />
282+
283+ define a class for batch file:<br />
284+
285+ class student
286+ {
287+ public string name { get; set; }
288+ public string lastname { get; set; }
289+ public int age { get; set; }
290+ public string shenasname { get; set; }
291+ public string url { get; set; }
292+ }
293+
294+ then call parsefile:<br />
295+
296+ student myObj = new student();
297+ student[] res = batch.ParseFile("e:\\data.txt", "e:\\syntax.xml", myObj);
298+
299+ foreach(student s in res)
300+ {
301+ Console.WriteLine("{0} {1} {2} {3} {4}", s.name, s.lastname, s.age, s.shenasname, s.url);
302+ }
303+
304+ Other Sample For Batch Parser:<br />
305+
306+ data.txt: (without boundary)<br />
307+
308+ alireza,paridar,0010000000,24,http://github.com/alirezap,ali,pari,0010000230,25,http://nuget.org/alirezap,
309+
310+ syntax.xml: (without boundary StartWith and EndWith value is null)<br />
311+
312+ <?xml version="1.0" encoding="UTF-8"?>
313+ <body>
314+ <syntax>
315+ <record StartWith="" EndWith=""/>
316+ </syntax>
317+ <content>
318+ <FIELD1 ID="1" Type="string" Title="name" TERMINATOR="," MAX_LENGTH="30"/>
319+ <FIELD2 ID="2" Type="string" Title="lastname" TERMINATOR="," MAX_LENGTH="30"/>
320+ <FIELD3 ID="3" Type="string" Title="shenasname" TERMINATOR="," MAX_LENGTH="30"/>
321+ <FIELD4 ID="4" Type="int" Title="age" TERMINATOR="," MAX_LENGTH="3"/>
322+ <FIELD5 ID="5" Type="string" Title="url" TERMINATOR="," MAX_LENGTH="50"/>
323+ </content>
324+ </body>
325+
326+ in the code:<br />
327+
328+ define a class for batch file:<br />
329+
330+ class student
331+ {
332+ public string name { get; set; }
333+ public string lastname { get; set; }
334+ public int age { get; set; }
335+ public string shenasname { get; set; }
336+ public string url { get; set; }
337+ }
338+
339+ then call parsefile:<br />
340+
341+ student myObj = new student();
342+ student[] res = batch.ParseFile("e:\\data.txt", "e:\\syntax.xml", myObj);
343+
344+ foreach(student s in res)
345+ {
346+ Console.WriteLine("{0} {1} {2} {3} {4}", s.name, s.lastname, s.age, s.shenasname, s.url);
347+ }
348+
349+
209350
210351
211352Refrence:
0 commit comments