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.
<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
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.