Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Easy Method to Split Large XML File Using LINQ to XML

0.00/5 (No votes)
23 Jun 2014 2  
Use LINQ to XML to split an XML file into a number of smaller files

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);
            // Child elements from source file to split by.
            var childNodes = xml.Descendants(descendantElement);

            // This is the total number of elements to be sliced up into 
            // separate files.
            int cnt = childNodes.Count();

            var skip = 0;
            var take = takeElements;
            var fileno = 0;

            // Split elements into chunks and save to disk.
            while (skip < cnt)
            {
                // Extract portion of the xml elements.
                var c1 = childNodes
                            .Skip(skip)
                            .Take(take);

                // Setup number of elements to skip on next iteration.
                skip += take;
                // File sequence no for split file.
                fileno += 1;
                // Filename for split file.
                var filename = String.Format(destFilePrefix + "_{0}.xml", fileno);
                // Create a partial xml document.
                XElement frag = new XElement(rootElement, c1);
                // Save to disk.
                frag.Save(destPath + filename);
            }
        }
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here