-4

I have a large xml and I need to split it to smaller one.

1 Answers1

1

With MoreLINQ (available from NuGet) you can split orders into batches, and then build new documents from batches:

XDocument xdoc = XDocument.Load("orders.xml");
int batchSize = 2;
int batchIndex = 0; 

foreach(var orders in xdoc.Descendants("order").Batch(batchSize))
{
    XDocument doc = new XDocument(new XElement("orders", orders));
    doc.Save(String.Format("orders-{0}.xml", batchIndex++));
}

This will create new documents, each will contain batchSize orders inside (well, last one can contain less orders). Orders will be placed under <orders> root tag.


You can even do all in single query

xdoc.Descendants("order").Batch(batchSize)
    .Select((orders, index) => new { orders, index })
    .ForEach(batch => {
        XDocument doc = new XDocument(new XElement("orders", batch.orders));
        doc.Save(String.Format("orders-{0}.xml", batch.index));
    });
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • Batch is not supported in the system.collection.Generic.Ienumerable – user1453017 Jul 29 '13 at 08:37
  • @user1453017 have you downloaded MoreLinq package and included `using MoreLinq;` namespace reference into your file? – Sergey Berezovskiy Jul 29 '13 at 08:38
  • `Batch` is rather easy to implement - as you yourself demonstrated in http://stackoverflow.com/a/13731823/168719 :) - and thus adding a whole separate library looks like an overkill here. There is no indication that the OP needs anything else out of it – Konrad Morawski Jul 29 '13 at 09:03
  • @KonradMorawski yep, that was me :) But library is very small (0.06 Mb) I think speed up of development worth using it. It's very hard to tell when you should start using library. If one feature (batching) is not enough, then how many features is enough - two or three? :) If you already implemented batching manually, and there is new requirement which involves DistinctBy - what would you do - delete all your tested implementation and add MoreLINQ, or just implement another method DistinctBy? Actually you are at the same dilemma as before batching - whether to add single method, or add library – Sergey Berezovskiy Jul 29 '13 at 10:19
  • @user1453017 if this solution fits your needs, than please mark it as accepted for others to see problem was solved. BTW for future - to avoid downvoting and closing question - always provide code which you have – Sergey Berezovskiy Jul 29 '13 at 10:20