Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Splitting a large XML file

Posted on 2004-04-10
4
Medium Priority
?
254 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 750 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 750 total points
ID: 10805102
If the code above consumes too much memory you should consider using XPath and XmlReader and XmlWriter.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

604 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