c# - Split list<t> into files by n number -


I have a list that contains more than 4000 items. These items are passed through a method which Returns the XML block as a string. I would then need to use the sticker to write these strings in the XML file. There is a limit of 1000 "string" permissions per file only.

  int elementCount = 0; Int filename = 1; String filename = "c: \\ test \\" + date time.Now (). ToString ("ddMMyyyy") + "_0" + File Number + ".xml"; What {StreamWriter sw = new streamer (filename); Foreach (in vehicles in vehicles) {if (elementCount! = 1000) {sw.WriteLine (ConvertCode.Toxml (v)); ElementCount ++; } Else {sw.Close (); FileNumber ++; Sw = new streamer (filename); ElementCount = 0; Sw.WriteLine (ConvertCode.ToXml (v)); ElementCount ++; break; }}} At the end of this code, I expect that there should be 5 files with 1000 items and 1 other file with the remaining balance of the item (elementCount & lt; 1000)  

4 files with In those moments, I'm getting only one file that has 500 items in the past and the last 500 items.

I know that there should be something wrong with my looping structure, but I can not work how to get a successful structure Thanks

I think the file name is incorrect. You have else in block:

  sw = new streamer (filename);  

As you can see, you create a new file (for new numbers) without changing the new name. I think it should be:

  var currDate = DateTime.Now (). ToString ("ddMMyyyy"); String filename = "c: \\ test \\" + + currDate + "_0" + file number + ".xml"; .... else {sw.Close (); FileNumber ++; FileName = "C: \\ test \\" + + currDate + "_0" + file number + ".xml"; Sw = new streamer (filename); ElementCount = 0; Sw.WriteLine (ConvertCode.ToXml (v)); ElementCount ++; // break; // This break is not necessary}  

What's more, you used the break which breaks the whole loop (brakes the brakes), and Re-introduces the revised vehicles). I think this is not necessary.

The last thing - to do outside ... while is also not necessary, I do not see a purpose.

BTW This is a good algorithm for parallel:

  var list = vehicles as IList & lt; Vehicle & gt; ?? Vehicles.ToList (); If any (return!!) Return; Var currDate = DateTime.Now (). ToString ("ddMMyyyy"); Var Group = List.Count / 1000; Parallel.For (0, group + 1, grouping = & gt; {var fileName = "C: \\ test \\" + currDate + "_0" + groupNo + ".xml"; (var sw = new streamer (filename) {Var i = groupNo * 1000, i & lt; border; i ++} {var v = list [i]; {var limit = Math.Min (group no * 1000 + 1000, list for sw.WriteLine) .count); ConvertCode.ToXml (v));}}});  

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -