Solved

Splitting a large XML file

Posted on 2004-04-10
4
249 Views
Last Modified: 2010-04-15
I have a large XML similar to the one below:

<BookStore>
<Book>book1</Book>
<Book>book2</Book>
.....
<Book>book1000</Book>
<Book>book1001</Book>
......
</BookStore>

The <Book> element may occur in 1000's. I want to create several smaller XML files each containing say only 250 <Book> elements. As far as I know, the two ways it could be done is either using XSLT or loading the entire XML into a DataSet and then create smaller subset DataSets. Since I am not an expert on either of them, any help is appreciated
0
Comment
Question by:nadarajan
4 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 250 total points
ID: 10800685
This console application gets XML file name as command line parameter and creates number of output XML files with names C:\1.xml, C:\2.xml ... It is very basic and doesn't contain exception handling, but it contains all required code. Create new C# console application and paste this code to it:

using System;
using System.Xml;
using System.Text;


namespace XMLSplit
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            if ( args.GetLength(0) == 1 )
                SplitXMLFile(args[0], 250);
        }

        static void SplitXMLFile(String sFileName, int nNodesInFile)
        {
            XmlDocument document = new XmlDocument();
            document.Load(sFileName);

            XmlNodeList nodes = document.GetElementsByTagName("Book");

            int nFiles = (nodes.Count + nNodesInFile - 1)/nNodesInFile;

            for ( int i = 0; i < nFiles; i++ )
            {
                int nStart = i*nNodesInFile;
                int nEnd = (i+1)*nNodesInFile - 1;
                if ( nEnd > nodes.Count - 1 )
                    nEnd = nodes.Count - 1;

                WriteOutputFile(i+1, nodes, nStart, nEnd);
            }
        }

        static void WriteOutputFile(int nFileNumber,
            XmlNodeList nodes, int nStart, int nEnd)
        {
            XmlDocument doc = new XmlDocument();

            StringBuilder s = new StringBuilder();
            s.Append("<?xml version=\"1.0\"?>\n");
            s.Append("<BookStore>\n");
            s.Append("</BookStore>");

            doc.LoadXml(s.ToString());

            for ( int i = nStart; i <= nEnd; i++ )
            {
                XmlElement bookElement = doc.CreateElement("Book");
                bookElement.InnerText = nodes[i].InnerText;

                doc.DocumentElement.AppendChild(bookElement);
            }

            doc.Save(String.Format("C:\\{0}.xml", nFileNumber));
        }
    }
}
0
 
LVL 10

Assisted Solution

by:ptmcomp
ptmcomp earned 250 total points
ID: 10805102
If the code above consumes too much memory you should consider using XPath and XmlReader and XmlWriter.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Angular JS Route 3 68
Turn on intranet settings 1 55
Close form "before" open 3 40
VB.NET 2008 - SQL Timeout 9 22
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now