Wednesday, February 18, 2009

XML comment contains invalid XML: Whitespace is not allowed at this location

Today, I encountered this error: "XML comment contains invalid XML: Whitespace is not allowed at this location".

The problem occured in a code similar to this one:

///
/// Calculates the area & the surface
///

public double CalculateAreaAndSurface()

* Please, note that this above is just a sample code to illustrate the error, not a meaningful function or well written.

So, when I started using this function, I called it like this ObjectInstance.CalculateAreaAndSurface(), and the Visual Studio(2005 or 2008) didn't show me the comment I have written, but the error instead: "XML comment contains invalid XML: Whitespace is not allowed at this location".

After a small research, I figured out that the problem in the comment was that there is the character "&" in it, which should be replaced with the escaped version "& amp;". Some other characters like < and > are in this list of forbidden characters and should be escaped too.