Click here to Skip to main content
15,881,204 members
Articles / Programming Languages / C# 3.5
Tip/Trick

Easy Method to Split Large XML File Using LINQ to XML

Rate me:
Please Sign up or sign in to vote.
4.50/5 (7 votes)
23 Jun 2014CPOL 38.1K   12   7
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>.

Image 1

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhow to change the header? Pin
Member 1215454012-Jan-18 0:20
Member 1215454012-Jan-18 0:20 
QuestionNeed to remove namespaces in xml files and then splitting Pin
parimal226710-Apr-15 21:17
parimal226710-Apr-15 21:17 
Thanks its working but it is unable to remove the nodes carrying namespaces.
need to split xml files which is having 1000 nodes,but these nodes have namespace( common namespaces ) in it I want to remove the namespaces its making trouble in splitting ,after splitting i want to add in separated files .

Is there any way to split with having namespaces please help

GeneralThank you Pin
naat_8019-Dec-14 11:24
naat_8019-Dec-14 11:24 
QuestionLarge file, large memory consumption Pin
wmjordan23-Jun-14 15:36
professionalwmjordan23-Jun-14 15:36 
AnswerRe: Large file, large memory consumption Pin
ravgill6624-Jun-14 0:47
ravgill6624-Jun-14 0:47 
GeneralRe: Large file, large memory consumption Pin
johannesnestler25-Jun-14 3:24
johannesnestler25-Jun-14 3:24 
GeneralRe: Large file, large memory consumption Pin
ravgill6626-Jun-14 3:36
ravgill6626-Jun-14 3:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.