Click here to Skip to main content
15,896,493 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

Please help me out what i am doing wrong,

i have to split a big xml file into small xml files based on the count of the nodes/element(action node count). While i am doing this i am getting Out of memory exception.

Sample Xml format is shown below but my Original file size is 890 MB with 2,90,973 actions nodes from below.

XML
<Parent ExternalID="CombinedTransactionsTest" GeneratedDate="2013-03-21">
<Action ActionID="4010" ActionType="Transaction.Add" >
    <Transaction EnteredDate="2013-03-14" PostedDate="2013-03-14" PostedBy="CONV">
        <Adjustment AccountID="S00000006" />
        <Taxlot AccountID="S00000006" SourceAccountID="S00000006" />
        <Parcel AccountID="S00000006" />
        <ParcelUnit AccountID="S00000006" />
    </Transaction>
</Action>
<Action ActionID="4011" ActionType="Transaction.Add" >
    <Transaction EnteredDate="2013-03-14" PostedDate="2013-03-14" PostedBy="CONV">
        <Adjustment AccountID="S00000006" />
        <Taxlot AccountID="S00000006"/>
        <Parcel AccountID="S00000006"/>
        <ParcelUnit AccountID="S00000006" />
    </Transaction>
</Action>
</Parent>



From front end(UI), the user is able to specify how many action <action> nodes should be in each split-ted files. For example, if i specify 10 actions per file then i need to have 10 actions per file.

To do the above requirement, i have written the code as below.

Code
C#
if (File.Exists(txtConvXmlFilePath.Text))
{
    bool exception = false;
    string FileName = Path.GetFileNameWithoutExtension(txtConvXmlFilePath.Text);
   
        if (!Directory.Exists("D:\\XmlSplitFiles\\"))
            Directory.CreateDirectory("D:\\XmlSplitFiles\\");

        var xDoc = XDocument.Load(txtConvXmlFilePath.Text);
        var xmls = xDoc.Root.Elements().ToArray();

        int fileCount = 1;
        var file = File.CreateText("D:\\XmlSplitFiles\\" + FileName + fileCount.ToString() + ".xml");

        StringBuilder sb = new StringBuilder();
        sb.Append("<").Append(xDoc.Root.Name);

        if (xDoc.Root.HasAttributes)
        {
            sb.Append(" ");
            var attval = xDoc.Root.Attribute("ExternalID");
            if (attval != null)
                sb.Append(attval).Append(" ");
            var attval2 = xDoc.Root.Attribute("GeneratedDate");
            if (attval2 != null)
                sb.Append(attval2).Append(" ");
            sb.Append(" ").Append(">");
        }
        int batchCount = Int32.Parse(txtActionCount.Text.ToString());
        file.Write(sb.ToString() + Environment.NewLine);
        for (int i = 0; i < xmls.Length; i++)
        {

            file.Write(xmls[i].ToString() + Environment.NewLine);

            if (i == batchCount)
            {
                file.Write(Environment.NewLine + "</Parent>");
                file.Flush();
                file.Close();
                batchCount = batchCount + Int32.Parse(txtActionCount.Text.ToString());
                fileCount++;
                file = File.CreateText("D:\\XmlSplitFiles\\"+ FileName + fileCount.ToString() + ".xml");
                file.Write(sb.ToString() + Environment.NewLine);
            }
            else if (i == xmls.Length - 1)
            {
                file.Write(Environment.NewLine + "</Parent>");
                file.Flush();
                file.Close();
                file = null;
            }
        }

        MessageBox.Show("Split files are stored at " + "D:\\XmlSplitFiles\\");
     
}



From the above code,

Line
var xDoc = XDocument.Load(txtConvXmlFilePath.Text); is the place where i am getting error.

Thank you

Please answer this as soon as possible.
Posted
Updated 6-May-14 23:33pm
v3

 
Share this answer
 
Comments
thatraja 7-May-14 5:41am    
5! I was about to suggest him the same thing.
ommi.chiru 7-May-14 5:43am    
Thank you DamithSL,

Here i am not splitting the file based on the size. i have to split based on the nodes.

i even tried read this artical before i start work, but this is not useful for me

Please provide yoiu==
 
Share this answer
 
Comments
Matt T Heffron 7-May-14 12:50pm    
+5

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