Introduction
You have a large well formed XML file which you wish to split into smaller manageable files. Each output file is also a well formed XML file. This approach uses Skip and Take LINQ extension methods to intuitively slice and dice the source XML into smaller parts.
Using the Code
Hopefully the code is sufficiently commented so that further explanation is not required.
The source XML file can be downloaded here.
You need to drop the source XML file into your "C:\temp" folder.
On running the code, the source file products.xml containing 504 elements of <Product>
are split across three files containing 200, 200, 104 elements of <Product>
.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml.Schema;
namespace SplitXmlFile
{
class Program
{
static void Main(string[] args)
{
string sourceFile = @"C:\Temp\Products.xml";
string rootElement = "Products";
string descElement = "Product";
int take = 200;
string destFilePrefix = "ProductsPart";
string destPath = @"C:\temp\";
SplitXmlFile(sourceFile, rootElement, descElement, take,
destFilePrefix, destPath);
Console.ReadLine();
}
private static void SplitXmlFile(string sourceFile
, string rootElement
, string descendantElement
, int takeElements
, string destFilePrefix
, string destPath)
{
XElement xml = XElement.Load(sourceFile);
var childNodes = xml.Descendants(descendantElement);
int cnt = childNodes.Count();
var skip = 0;
var take = takeElements;
var fileno = 0;
while (skip < cnt)
{
var c1 = childNodes
.Skip(skip)
.Take(take);
skip += take;
fileno += 1;
var filename = String.Format(destFilePrefix + "_{0}.xml", fileno);
XElement frag = new XElement(rootElement, c1);
frag.Save(destPath + filename);
}
}
}
}