Cleaning up after setting XML data.
-
03 Nisan 2012 Salı 18:43
I have some ADO.NET code using C# that is as follows:
command.Parameters.Add(new SqlParameter("@x", SqlDbType.Xml)); command.Parameters["@x"].Value = new SqlXml(responseDocumentReader);
The 'responseDocumentReader' is an XmlReader which is created using XmlReader.Create(Stream stream). So my question is what happens when the command is successfully executed? I am assuming that the instance of SqlXml will be disposed of. What happens to the XmlReader? Is it closed of displosed or both. What about the base stream of the XmlReader? I have built a class that overrides Dispose so I can remove the associated file when it is no longer used. It seems that the file is not getting removed. My first conclusion would be that somehow the underlying Stream is not Disposed of.
Any pointers?
Thank you.
Kevin Burton
Tüm Yanıtlar
-
04 Nisan 2012 Çarşamba 07:25Moderatör
Hi Kenvin,
Welcome!
I think you just need to call XmlReader's Dispose method and it will help you to dispose Stream object automatically. This is a depandence issue in GC. If you are instead of it, you can refer here: http://www.softwareni.com/dotnet/blog/file.axd?file=2011%2F4%2FCLR+via+Csharp.pdf --21 chapter(Page 554).
Have a nice day.
Alan Chen[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

-
04 Nisan 2012 Çarşamba 14:22
I need to explicitly call the Dispose method on the XmlReader. It will not get called when the SqlXml instance is disposed of?
Kevin
Kevin Burton
-
06 Nisan 2012 Cuma 05:50Moderatör
Hi Kevin,
When you use SqlXml, the XML value that you assign to the SqlXml instance must be consumable by an XmlReader. You can refer here: http://msdn.microsoft.com/en-us/library/system.data.sqltypes.sqlxml.aspx
Have a nice day.
Alan Chen[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

-
09 Nisan 2012 Pazartesi 14:31So my question is what happens to the XmlReader when the SqlXml instance is disposed of?
Kevin Burton
-
11 Nisan 2012 Çarşamba 09:10Moderatör
Hi Kevin,
When you reflector SqlXml type, you will find the answer:
public SqlXml(XmlReader value) { if (value == null) this.SetNull(); else { this.m_fNotNull = true; this.firstCreateReader = true; this.m_stream = this.CreateMemoryStreamFromXmlReader(value); } }You should close the XmlReader explictly or wait for GC.
Have a nice day.
Alan Chen[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

- Yanıt Olarak İşaretleyen KevinBurton 11 Nisan 2012 Çarşamba 13:27
-
11 Nisan 2012 Çarşamba 13:27
Thank you. I didn't think of using Reflector.
This brings up a few other questions. One, if as the name implies, it is creating a memory stream from the XmlReader then it may be reading the whole XML document into memory. If the document is long (say 1-2 Gb) then that could result in considerable memory and CPU (swapping and paging) pressure. Something I had hoped to avoid. But I guess it is good to know. Two, there is little detail on how the memory stream is created. For all we know based on the code snippet the XmlReader could be close and/or disposed after the memory stream is created. I guess I will have to do some more sleuthing. But I will take your word for it and explicitly close/dispose of the XmlReader.
Thanks again.
Kevin Burton