Document .NET Libraries with XML Comments

Any development environment spawns standards. In the case of .NET, one of these standards is the way that developers expect class library documentation to look. Ideally, your class library documentation should follow the format and conventions that the .NET Framework SDK established for .NET namespaces.

Fortunately, this isn't all that hard to do, at least if you're working in C# (VB .NET will gain the same capabilities in Visual Studio 2005). A combination of the XML comments feature built into the language and the open source NDoc project makes it easy to generate high-quality SDK-style help. I'll show you the basics in this article.

Adding XML Comments

The key to the whole process is the open source tool NDoc. NDoc is actually the end of a process that starts with Appendix B of the C# language specification: "C# provides a mechanism for programmers to document their code using a special comment syntax that contains XML text. In source code files, comments having a certain form can be used to direct a tool to produce XML from those comments and the source code elements, which they precede." These comments are identified by being set off with three slashes instead of the two that normally introduce a C# comment.

The XML Documentation section of the C# Programmer's Reference documents tthe XML tags that are used by these special commants.. Here are the XML documentation tags that Visual Studio .NET can use for XML documentation:

  • <c>—Embedded code font in other text
  • <code>—Multiple lines of source code
  • <example>—An example of using a member
  • <exception>—Specifies an exception that can be thrown by the current member
  • <include>—Includes an external documentation file
  • <list>—The heading row of a list
  • <para>—A paragraph of text
  • <param>—A method parameter
  • <paramref>—A reference to a parameter in other text
  • <permission>—The security permission required by a member
  • <remarks>—Supplemental information
  • <returns>—Return value of a method
  • <see>—An embedded cross-reference to another member
  • <seealso>—A reference in a See Also list
  • <summary>—A summary of the object
  • <value>—The value of a property

This list isn't fixed by the C# specification; different tools are free to make use of other tags (and in fact NDoc understands a few tags that are not included on the standard list).

As an example, here's some sample XML documentation embedded directly withing a C# library:

using System;

namespace XMLDoc
{
   /// <summary>
   /// This class contains static utility functions for use
   /// throughout the company.
   /// </summary>
   /// <remarks>
   ///      <para>As we develop general-purpose utility functions,
   ///            we will add them to this class.</para>
   ///      <para>The current utility functions include:</para>
   ///      <list type="bullet">
   ///         <item><see cref="FixQuotes" /> to fix up SQL
   ///               statements.</item>
   ///         <item><see cref="ReplaceNull" /> to convert nulls to
   ///               specified replacement valuess.</item>
   ///      </list>
   /// </remarks>
   public class Utility
   {
      /// <summary>
      ///      Static constructor
      /// </summary>
      static Utility()
      {
         // Nothing to do here
      }

      /// <summary>
      ///      Fix up a SQL statement by escaping single quotes
      /// </summary>
      /// <param name="SqlStatement" type="string">
      ///      <para>
      ///         A SQL statement, possibly with unescaped single quotes.
      ///      </para>
      /// </param>
      /// <returns>
      ///      Returns the SQL statement with any single quotes doubled.
      /// </returns>
      public static string FixQuotes(string SqlStatement)
      {
         return SqlStatement.Replace("'", "''");
      }

You get the idea, I'm sure. The XML comments contain all of the information that you'd like to see in a help file, and the tags provide all the information that's needed for the right tool to structure that file.

From Comments to Documentation

Embedded in your code, these comments don't do a lot of good for your customers. But Visual Studio .NET can collect these comments into an external XML file. To enable this collection, right-click on the project in Solution Explorer and select Properties. Then select the Build page and enter a name for the XML documentation file. By default, this file will be placed in the same folder as the compiled application.

After you've built the XML comments file for your application, NDoc can do its work. Figure 1 shows the NDoc user interface.

You can select one or more assemblies to document, and tell NDoc where to find the XML comments file for each assembly. NDoc will combine the information in the XML comments file with information determined by examining the assembly itself, and build MSDN-style documentation for you. Figure 2 shows a part of the resulting help file.

This little example shows both the ease of use and simplicity of NDoc, but there's more power working there if you need it. NDoc has implemented a concept of pluggable documenters, so you can use the same XML comments to get out LaTEX markup, HTML Help, and other formats in addition to the MSDN standard. And, of course, it's open-source so you could even add more functionality if you need it.

With these simple tools at your disposal, there's no reason to put out a .NET class library for consumption by other developers without it having professional-looking documentation. These days, the successful developers are the ones who are willing to do more than just write code. Coming up with documentation is often an essential part of the job as well.

About the Author

Mike Gunderloy is the author of over 20 books and numerous articles on development topics, and the lead developer for Larkware. Check out his latest book, Coder to Developer from Sybex. When he's not writing code, Mike putters in the garden on his farm in eastern Washington state.

IT Offers

Comments

  • purchase cheap Phentermine online

    Posted by wahbeadib on 05/20/2013 08:24pm

    Food habits, Healthy Food, Exercises, Life style, Ailments, Leave a comment Diabetes, Food, Obesity, Uncategorized, health tips, migraine, weight loss As opposed to some clinical trials that are done using mice, DEX-C20 was actually tested on humans and produced a good, safe weight loss overall. purchase Phentermine online order Phentermine 50mg online. Use the Sauna Belt -- it is an alternative way to burn your fats and lose weight! [url=http://www.buyphentermineonlinemed.com/#538491-phentermine-cheap]order cheap Phentermine online without a prescription[/url] US provides complete information about weight loss and losing fat.

    Reply
  • buy cheap Klonopin online without a prescription

    Posted by theogaphele on 05/19/2013 07:08am

    Buy Cheap Klonopin Online Without Prescription Order Klonopin Generic - Purchase Clonazepam Generic Cheap

    Reply
  • order Effexor online no prescription

    Posted by louffisee on 05/13/2013 04:23pm

    effexor Without a Prescription Cheap Purchase effexor Without Rx Cheap - Buy effexor Without a Prescription

    Reply
  • buy ambien 20mg online

    Posted by dowecticore on 05/06/2013 10:41am

    Purchase Zolpidem Without Prescription Cheap Ambien Online No Rx - Ambien Online Without Prescription Cheap

    Reply
  • where to buy accutane in singapore

    Posted by Scamocarar on 04/08/2013 12:18am

    how much does a prescription of accutane cost where to buy accutane online yahoo answers [url=http://ryousid.eklablog.fr/#accutane-cost-generic]where to buy cheap accutane[/url] cheap accutane no prescription [url=http://bartto.ek.la/#online-prescription-for-accutane]how to buy accutane cheap[/url] accutane generic cost without insurance [url=http://rattfunc.eklablog.com/#accutane-online-cheap]accutane purchase online no prescription[/url] accutane to buy online [url=http://tiablad.lo.gs/#buy-accutane-south-africa]how long does accutane stay in your system after last dose[/url] reliable online pharmacy accutane [url=http://fifthstig.revolublog.com/#online-prescription-for-accutane]does accutane come in generic form[/url] how long after taking accutane can i give blood what happens if you take accutane for too long

    Reply
  • best price propecia generic

    Posted by Litolara on 04/05/2013 01:54am

    propecia tab 1mg x 28 x 6 will generic propecia available us [url=http://anbun.eklablog.net/#buying-propecia-online-safe]where can i buy propecia cheap[/url] safe website buy propecia [url=http://raynie.ek.la/#propecia-buy-online-cheap]can you buy propecia uk[/url] is generic propecia available in the united states [url=http://firssent.lo.gs/#buy-propecia-finasteride-1mg]propecia buy online no prescription[/url] how to order propecia [url=http://gendmic.eklablog.com/#buy-cheap-propecia-no-prescription]generic propecia 1 mg[/url] best place buy propecia uk [url=http://tiopref.revolublog.com/#safe-to-buy-propecia-online]safe buy generic propecia[/url] do you need prescription generic propecia buy propecia nhs

    Reply
  • order nolvadex canada

    Posted by Olovdicon on 04/04/2013 03:38am

    what's better for pct clomid or nolvadex buy post cycle therapy nolvadex [url=http://afin.ek.la/#who-sells-nolvadex]generic nolvadex online[/url] buy cheap nolvadex [url=http://slancha.lo.gs/#purchasing-nolvadex-online]buy nolvadex uk paypal[/url] can you buy nolvadex online [url=http://voybal.eklablog.com/#buy-nolvadex-pct]buy liquid nolvadex[/url] how to buy nolvadex online [url=http://deste.eklablog.net/#nolvadex-prices]buy cheap valium online no prescription[/url] nolvadex clomid sale [url=http://vigin.eklablog.fr/#buy-nolvadex-in-australia]buy nolvadex review[/url] can buy nolvadex how much nolvadex to take during a cycle

    Reply
  • where to buy doxycycline online

    Posted by Tinlidipen on 04/02/2013 06:08pm

    doxycycline hyclate 100mg used for how to buy doxycycline in uk [url=http://razhi.eklablog.fr/#buy-doxycycline-online-no-prescription-uk]doxycycline malaria tablets buy[/url] doxycycline hyclate capsules 100 mg [url=http://teral.ek.la/#buy-doxycycline-in-bali]doxycycline hyclate 100 mg tab[/url] doxycycline hyclate false positive drug test [url=http://netpna.eklablog.net/#doxycycline-prices-canada]doxycycline hyclate buy[/url] can i buy doxycycline over the counter in thailand [url=http://laiso.revolublog.com/#price-doxycycline-20mg]generic periostat doxycycline hyclate[/url] buy doxycycline hyclate 100mg acne [url=http://exar.lo.gs/#doxycycline-50-mg-cost]do you need a prescription for doxycycline hyclate[/url] buy doxycycline south africa much does generic doxycycline cost

    Reply
  • yotTQm uU Ov uZY hfSs Yp

    Posted by YFsSXlNNbw on 04/02/2013 06:15am

    buy tramadol online no prescription cheap tramadol online inrikes - купить tramadol online

    Reply
  • SABpe GDr UErB

    Posted by EOKDjyyOwC on 03/21/2013 05:57am

    advices how to buy tramadol online 100mg ultram tramadol ingredients - tramadol tablets dogs dosage

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Go Deeper

  • This buyers guide provides independent research and test results to help you determine your endpoint protection requirements and identify …
  • This interactive white paper from CIO Magazine and EMC lays out the benefits of big data and predictive analytics, provides tips on how to …
  • This online eBook provides insight and advice on how to build an effective disaster recovery strategy in the evolving world of virtual …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds