Search code examples
c#asp.netroslyn-code-analysisxml-documentation

Allow ampsersands in c# doccomments and avoid analyzer warning CS1570


I have this:

/// <summary>
/// Foo
/// </summary>
/// <seealso href="https://www.example.com/foo?q=bar&qux=2"/>

That gives:

XML comment has badly formed XML -- 'Reference to undefined entity 'qux'.' csharp(CS1570)

The problem is the & is invalid XML.

Workarounds:

  • convert & to &amp;
  • disable the warning with a #pragma warning disable CS1570
  • from comments below: <NoWarn>1570</NoWarn>
  • from comments below: find (using regex) and replace & with &amp;

These are non ideal and a waste of time - I just want to paste my URLs into the code and move on.

Is it possible to configure the analyzer to allow ampersands?


Solution

  • While you could disable this warning by adding <NoWarn>1570</NoWarn> to your .csproj file, this is also not an ideal solution, as the compiler will ignore documentation comments that have invalid XML - such comments will not be included in the compiler's XML output, and will not show up in tooltips in IDEs.

    The best solution, if you are pasting a lot of URLs into your comments and don't want to fix them yourself, would be to do a find-and-replace on your source files to fix the invalid &s to &amp;. This could be done as a pre-build event so that it runs before every build.