Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
2.09/5 (3 votes)
See more:
Hello,


i have a big XML file,which i want to split in several files based on Element count
Ex. if the file is like below

XML
<bookstore>
    <book category="...">
        <title >...</title>
        <author>...</author>
        <year>...</year>
        <price>...</price>
    </book>
    <book category="...">
        <title >...</title>
        <author>...</author>
        <year>...</year>
        <price>...</price>
    </book>
    <book category="...">
        <title >...</title>
        <author>...</author>
        <year>...</year>
        <price>...</price>
    </book>
    <book category="...">
        <title >...</title>
        <author>...</author>
        <year>...</year>
        <price>...</price>
    </book>
</bookstore>


each file should contain 2 books only Therefore it should generate two files like below
XML
<bookstore>
    <book category="...">
        <title >...</title>
        <author>...</author>
        <year>...</year>
        <price>...</price>
    </book>
    <book category="...">
        <title >...</title>
        <author>...</author>
        <year>...</year>
        <price>...</price>
    </book>
</bookstore>



Any ideas?
Posted
Updated 11-Oct-18 19:00pm

i did it this way
C#
//New xml document object
                XmlDocument newXmlDoc = new XmlDocument();
                //XML writer to be used
                XmlTextWriter Xwr ;
//Read Root Element from XML
                XmlElement root = Doc.DocumentElement;

                //node nae to load nodes list
                if (Doc.DocumentElement.ChildNodes.Count > 0)
                    RootNodeName = "//" + root.Name + "/" + Doc.DocumentElement.ChildNodes[0].Name;
                else
                    RootNodeName = "//" + root.Name;

                //load all client noded in Node List
                XmlNodeList NodeList = Doc.SelectNodes(RootNodeName);
              

                //crete new doc and write empty root node
                XmlNode CurrentNode = null;
                XmlNode RootNode = newXmlDoc.CreateElement(root.Name);
                newXmlDoc.AppendChild(RootNode);


                for (int i = 0; i < NodeList.Count; i++)
                {
                    CurrentNode = NodeList[i];

                    //to skip empty nodes
                    if (CurrentNode.InnerXml == null || CurrentNode.InnerXml.Trim() == "")
                        continue;

                    //append nodes till batch size is reached
                    if (RecCount < iBatchSize)
                    {
                        XmlNode targetNode = newXmlDoc.ImportNode(CurrentNode, true);
                        RootNode.AppendChild(targetNode);
                        RecCount = RecCount + 1;
                    }
                    else
                    {
                        //once batch size is reached, save file and re-initialise xml document
                        FileCount = FileCount + 1;
                        Xwr = new XmlTextWriter(FilePath + @"\" + FileName + "_" + FileCount.ToString() + ".xml", Encoding.UTF8);
                        Xwr.Formatting = Formatting.None;
                        newXmlDoc.Save(Xwr);
                        Xwr.Close();
                        Xwr = null;
                        newXmlDoc = null;

                        newXmlDoc = new XmlDocument();
                        RootNode = newXmlDoc.CreateElement(root.Name);
                        newXmlDoc.AppendChild(RootNode);
                        XmlNode targetNode = newXmlDoc.ImportNode(CurrentNode, true);
                        RootNode.AppendChild(targetNode);
                        RecCount = 1;
                    }
                    if (i == NodeList.Count - 1)
                    {
                        if (RecCount > 0)
                        {
                            //if remaining items are less than Batchsize, save file at last
                            FileCount = FileCount + 1;
                            Xwr = new XmlTextWriter(FilePath + @"\" + FileName + "_" + FileCount.ToString() + ".xml", Encoding.UTF8);
                            Xwr.Formatting = Formatting.None;
                            newXmlDoc.Save(Xwr);
                            Xwr.Close();
                            Xwr = null;
                            Console.WriteLine();
                            newXmlDoc = null;
                        }
                    }

                }
 
Share this answer
 
Comments
Member 13475091 12-Oct-18 1:01am    
Would you be able to post the declaration part of the code please.I'm having to split a 4GB xml file.

Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900