I have the function below which is used to serialize an object without adding the XML declaration. I\'ve just opened the project containing it an Visual Studio 2012 and the
It is a false warning, caused by XmlWriter disposing the stream you pass. Which makes your StringWriter disposed twice, first by XmlWriter and again by your Using statement.
This is not a problem, disposing .NET framework objects twice is not an error and doesn't cause any trouble. It could be a problem if the Dispose() method is poorly implemented, FxCop doesn't take its chances to not tell you about it because it isn't otherwise smart enough to know if a Dispose() method is correct.
There is not any way to rewrite the code to avoid a warning. StringWriter doesn't actually have anything to dispose so moving it out of the Using statement is okay. But that will produce another warning, CA2000. Best thing to do is to just ignore this warning. Use the SuppressMessageAttribute if you don't want to look at it again.
Try to modify the code to have 2 separate usings:
Using stream As New StringWriter()
Using writer As XmlWriter = XmlWriter.Create(stream, settings)
End Using
End Using