Solved

Splitting a large XML file

Posted on 2004-04-10
4
246 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
Comment Utility
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
Comment Utility
If the code above consumes too much memory you should consider using XPath and XmlReader and XmlWriter.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 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

6 Experts available now in Live!

Get 1:1 Help Now