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.

11 comments:

Abhijit P said...

Thanks a lot for the tip. Helped me solve my issue quickly.

Shaunak said...

Cheers was having the same issue for the same reason.

Anonymous said...

Thank you for this post, it's really useful to know ;)

AASHISH said...

Thank you very much.

Anonymous said...

Thanks a lot. Really useful tip...:)

Anonymous said...

Actually as per the XML spec (http://www.w3.org/TR/xml/#sec-comments) the ampersand, and other characters are valid for comments. The only thing that can't appear in a comment is the double hyphen (--).

Anonymous said...

Thanks for sharing such a beneficial tip... :-)

nivekct said...

An insightful post... This is because in your Column fields that you pass to the Xml there is ampresant and in XML its a special character, now you need to turn it off... Outlook invalidxml

nivekct said...

An insightful post... This is because in your Column fields that you pass to the Xml there is ampresant and in XML its a special character, now you need to turn it off... Outlook invalidxml

Anonymous said...

great help, thanks

Anonymous said...

Very helpful, thanks