Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# XML
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
 
<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
<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 3-May-13 2:24am
Amol_B6.4K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

i did it this way
//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;
                        }
                    }
 
                }
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 325
1 DamithSL 265
2 CPallini 235
3 Maciej Los 190
4 Sergey Alexandrovich Kryukov 189
0 OriginalGriff 5,455
1 DamithSL 4,422
2 Maciej Los 3,860
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,010


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 8 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100