XmlWriter - sometimes doesnt close the end of the file with the closing tag

I am writing a large file to xml   - by large I am meaning 32 - 40 mgs and sometimes it does not white the closing tag - can anyone see why?
 XmlWriter writer = XmlWriter.Create(path + "\\" + jobID + ".xml");
                    writer.WriteStartDocument();
                    writer.WriteStartElement("Surveys");
                    List<InvictusRTDatabase.ChartData> mySurveys = new List<InvictusRTDatabase.ChartData>();
                    mySurveys = irtd.getAllChartDataForSurveys(Convert.ToInt32(jobID));
                    foreach (var survey in mySurveys)
                    {
                        writer.WriteStartElement("Survey");
                        writer.WriteElementString("SurveyID", survey.SurveyID.ToString());
                        writer.WriteElementString("ChartDataUID", survey.ChartDataUID.ToString());
                        writer.WriteElementString("ChartDataTime", survey.ChartDataTime.ToString());
                        writer.WriteElementString("FilteredDataPoint", survey.filteredDataPoint.ToString());
                        writer.WriteElementString("rawDataPoint", survey.rawDataPoint.ToString());
                        writer.WriteElementString("Centroid", survey.Centroid.ToString());
                        writer.WriteElementString("CentroidOffset", survey.CentroidOffset.ToString());
                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();
                    writer.WriteEndDocument();

Open in new window

LVL 6
r3nderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Russ SuterSenior Software DeveloperCommented:
Make sure you call writer.flush() before you finish up.

https://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.flush(v=vs.110).aspx

Also, because XmlWriter implements iDisposable you would be better off employing a using clause.

using (XmlWriter writer = XmlWriter.Create(path + "\\" + jobID + ".xml"))
{
                    writer.WriteStartDocument();
                    writer.WriteStartElement("Surveys");
                    List<InvictusRTDatabase.ChartData> mySurveys = new List<InvictusRTDatabase.ChartData>();
                    mySurveys = irtd.getAllChartDataForSurveys(Convert.ToInt32(jobID));
                    foreach (var survey in mySurveys)
                    {
                        writer.WriteStartElement("Survey");
                        writer.WriteElementString("SurveyID", survey.SurveyID.ToString());
                        writer.WriteElementString("ChartDataUID", survey.ChartDataUID.ToString());
                        writer.WriteElementString("ChartDataTime", survey.ChartDataTime.ToString());
                        writer.WriteElementString("FilteredDataPoint", survey.filteredDataPoint.ToString());
                        writer.WriteElementString("rawDataPoint", survey.rawDataPoint.ToString());
                        writer.WriteElementString("Centroid", survey.Centroid.ToString());
                        writer.WriteElementString("CentroidOffset", survey.CentroidOffset.ToString());
                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();
                    writer.WriteEndDocument();
                    writer.flush();
}

Open in new window


(did that off the top of my head without checking it in the Visual Studio compiler first but I think I got it right)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
r3nderAuthor Commented:
Thanks you where right
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.