xmlWriterSettings := xmlWriterSettings.XmlWriterSettings(); xmlWriterSettings.Indent := TRUE; xmlWriter := xmlWriter.Create('c:\temp\nav.xml', xmlWriterSettings); xmlWriter.WriteStartElement('Items', 'http://mynamspace'); FOR i := 1 TO 200000 DO BEGIN xmlWriter.WriteStartElement('InventoryRecord'); xmlWriter.WriteElementString('ItemNo', 'ITM000001'); xmlWriter.WriteElementString('ItemNo2', 'ITM000001'); xmlWriter.WriteElementString('ItemNo3', 'ITM000001'); xmlWriter.WriteElementString('ItemNo4', 'ITM000001'); xmlWriter.WriteElementString('ItemNo5', 'ITM000001'); xmlWriter.WriteElementString('ItemNo6', 'ITM000001'); xmlWriter.WriteEndElement(); END; xmlWriter.WriteEndElement(); xmlWriter.Flush(); xmlWriter.Close();
using System; using System.Xml; namespace ConsoleApplication4 { class Program { static void Main(string[] args) { XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; using (XmlWriter writer = XmlWriter.Create(@"c:\temp\inventory.xml", settings)) { writer.WriteStartElement("Items", "http://mynamspace"); for (int i = 0; i < 200000; i++) { writer.WriteStartElement("InventoryRecord"); writer.WriteElementString("ItemNo", "ITM000001"); writer.WriteElementString("ItemNo2", "ITM000001"); writer.WriteElementString("ItemNo3", "ITM000001"); writer.WriteElementString("ItemNo4", "ITM000001"); writer.WriteElementString("ItemNo5", "ITM000001"); writer.WriteElementString("ItemNo6", "ITM000001"); writer.WriteEndElement(); } writer.WriteEndElement(); writer.Flush(); } Console.WriteLine("done"); Console.ReadLine(); } } }
Comments
First of all, thank you for posting your feedback!
We are sorry to hear you ran into this issue and feel this way.
I understand that you have also logged a case with us in this regards, and my colleague is currently working on it.
His feedback to me was that he was able to reproduce and is in the process of creating a CR with the DEV team to check if this is a problem.
We will contact you back via our support case as soon as we have updates, but please do let me know if there is anything more I can assist with at this point.
Thanks!
Support Engineer
Microsoft Dynamics NAV
Follow EMEA Microsoft Dynamics NAV Support on Twitter @MSDynNAVSupport!
Furthermore, the XmlWriter class allocates the unmanaged resources, making it mandatory to clean them up, using the Dispose method.
A quick fix could be to make the XmlWriter a local variable in a function, and do all the calls locally. If Microsoft documentation is still valid, they will call the Dispose method for you if a variable is an IDisposable. This was implicit in .NET Interop in NAV 2009 R2, when all local DotNet variables were used in a using block in C#. In NAV 2013 it's a bit different, so maybe this call to Dispose happens somewhere else, explicitly, I can't check that.
Another quick fix would be to call XmlWriter.Dispose immediately after you call XmlWriter.Close - that will clean up the resources. However, calling Dispose directly can have unexpected side effects, because NAV might be calling it for you if it is a local variable - check the first solution, then the second one.
I hope this helps.
http://vjeko.com/
With this information I've created the two functions below.
Memory consumption is now pretty low, CPU consumption is higher (because the XmlWriter will be created and disposed very often, I think), but that's not a problem in my case.
In the first function I initialize a global stream variable and write the "xml-frame" with a global XmlWriter.
Inside the loop I call the second function.
The second function creates a local XmlWriter an writes some records to the global stream.
When the second function finishes, the "local resources" will be released.
It's weird, to write to the same stream from multiple XmlWriters, and you can end up with invalid xml pretty fast, but as long as it works I'm fine with this.
I'm using one global stream variable, one global XmlWriter and a lot of local XmlWriter.
All the XmlWriter write to the global stream.
Because the local XmlWriter goes out of scope everytime the function ends, there happen some kind of memory cleanup.