Using NDoc: Adding World-Class Documentation to Your .NET Components
Pages: 1, 2
Using the <include> Tag To Keep Docs Out of Sight, But Not Out of Mind
As I mentioned earlier, one of the traditional problems with source-code-based documentation generators is that in order to write full-fledged documentation for your product, you must swamp your code in comments. (Too many comments? Yes indeed, it is always possible to have too much of a good thing.)
The doc-comment feature of the C# compiler addresses this problem nicely by
allowing you to keep the meat of your documentation in a separate XML
file. You need only insert one line of doc-comment code, the
<include>
element, to link that part of your source code to the appropriate bits in your
XML doc.
Use of the
<include>
element requires you to specify two attributes: the relative path to the
XML file, and an XPath expression to point to the XML node(s) to include.
If you're new to XPath, this is a perfect opportunity to learn this important
bit of XML technology -- see the References section
for links to some XPath tutorials.
/// <include file='myrefdocs.xml'
/// path='//Class[@name="Foo"]/summary'
/// />
/// <include file='myrefdocs.xml'
/// path='//Class[@name="Foo"]/remarks'
/// />
/// <include file='myrefdocs.xml'
/// path='//Class[@name="Foo"]/seealso'
/// />
public class Foo
{ ... }
The XPath expressions in the above example grab the
<summary>,
<remarks>, and
<seealso>
child elements from all
<Class>
elements with the
name="Foo"
attribute. I just made this up -- you're free to invent your own XML
grammar to organize your documentation.
<?xml version="1.0"?>
<Library>
<Namespace name="Arithex.Samples">
<Class name="Foo">
<summary>This is a sample class.</summary>
<remarks>
Here are some interesting remarks about the Foo sample class.
</remarks>
<seealso cref="Bar" />
</Class>
</Namespace>
</Library>
For serious software engineering projects, it's well worth your time to make
use of the
<include>
element in this way. You can split the documentation up among multiple
XML files, in any way you wish -- you'll be able to hand off these XML doc files
to a team of tech writers, or even other engineers, who'll be able to check the
files in and out of source code control independent of the actual C# source
code, thus avoiding many merging-related headaches for you or your team.
For example, in a project I'm currently working on, we've decided to split the
XML documentation into separate peer files for each C# file -- where Foo.cs
would be accompanied by a Foo.xml file, which contains the meat of the
documentation. For smaller projects, keeping all of the docs in single XML
file may serve you better. You can even choose to mix and match (perhaps
keeping the
<summary>
elements inline in your C# code, and everything else out-of-line in a separate
XML file).
NDoc Extensions to C#'s XML Documentation Tags
There are a few important bits missing from Microsoft's specification of C#'s XML Documentation feature. The most notable of these is a way to specify a common summary for the overloads of a function. This is where NDoc really comes into its own: because the format of the docs output by the C# compiler is XML, it's trivial for NDoc to extend the schema to include any missing bits. The C# compiler doesn't complain -- it will pass through any XML tags it doesn't recognize into the resulting documentation, where they can be interpreted by third-party tools (like NDoc).
NDoc extends the set of Microsoft-defined tags by defining a new
<overloads>
tag, a new
<events>
tag for documenting the events raised by a member function, and extending the
grammar of the
<see>
and
<seealso>
tags to allow linking to arbitrary URLs (not just other code items).
You can find some rudimentary documentation for these extended tags in the
tags.html
file, which is included in the NDoc distribution. The
<overloads>
tag requires a bit of explanation -- it comes in two flavors: simple and
complex.
The simple form of
<overloads>
is used very much like a
<summary>
tag, to provide a short description string for the suite of overloaded
methods. (If you neglect to provide an
<overloads>
tag, NDoc will use the
<summary>
tag on the first overload to describe the overloaded method.)
Figure 1: NDoc-generated text for an overloaded method
The more complex form of
<overloads>
allows you to specify other common documentation items, such as a
<remarks>
section. Unfortunately, it's not a general-purpose dumping ground for all
documentation items that are relevant to the individual overloaded methods --
you can't, for example, place documentation for the overloads' common
parameters in the
<overloads>
element and expect them to show up. However, with a little creative
application of the
<include>
tag, you can easily achieve this effect. The sample project accompanying
this article demonstrates the combined use the
<overloads>
and
<include>
tags, for an overloaded method named Solve:
<Method name="Solve">
<docs>
<overloads>Attempts to solve the function.</overloads>
<param name="guessX1">An initial guess.</param>
<param name="guessX2">Another initial guess.</param>
<param name="resultPrecision">
Desired precision for solution.
</param>
<param name="resultStatus">
The outcome of the solution.
</param>
<returns>
The root of the equation (or Double.NaN, upon error).
</returns>
</docs>
</Method>
<Method name="Solve1">
<docs>
<summary>Attempts to solve the function, using the default
max. number of iterations.</summary>
<remarks>Attempts to solve the function, using the default
max. number of iterations.</remarks>
</docs>
</Method>
<Method name="Solve2">
<docs>
<summary>Attempts to solve the function, using the specified
max. number of iterations.</summary>
<remarks>Attempts to solve the function, using the specified
max. number of iterations.</remarks>
<param name="maxIterations">
Maximum number of iterations to search.
</param>
</docs>
</Method>
Linking to External Documentation
One of the nicer features of NDoc is the way it provides seamless links to documentation in the .NET Framework SDK -- it even goes so far as to extract XML documentation for members inherited from their respective assemblies' XML doc-comment files -- but it can only provide links for the types it knows about.
For other types -- types defined in third-party assemblies -- you're on your own. That's not to say the situation is impossible, just that you have to do a little work.
Let's say you have a method with a parameter of type AcmeSoft.Foo, defined in
AcmeSoft.dll. You know that AcmeSoft has wonderful documentation for
their product, available online at acmesoft.com, but there's a problem:
NDoc will generate a hyperlink from your method's page to a non-existent help
topic
"AcmeSoft.Foo.html". If you try to follow this
link in the HtmlHelp browser, you'll be greeted with an error message.
That page doesn't exist in your CHM file, because that type doesn't exist
anywhere in your assembly, so NDoc didn't generate it. To rectify the
situation, all you need to do is supply the missing ingredient:
author an HTML page (manually), name it "AcmeSoft.Foo.html," and pre-place it
into the NDoc output directory before running NDoc (or at least before running
the HtmlHelp compiler).
It's a bit of a hack -- relying on an accident of NDoc's implementation, perhaps -- but it works, until a future version of NDoc offers a better alternative. And by writing your own page, you're allowed the flexibility you need to warn the user that they're about to start reading third-party documentation, and/or that you're about to connect to them to the Internet, etc.
Conclusion
With each new project I work on, I grow continuously more impressed with the flexibility and versatility of NDoc. It's saved me hundreds of hours of tedious labor engineering my own MSDN-style documentation solution, and it looks good doing it!
For a jumpstart into the wonderful world of NDoc, download the sample project accompanying this article, and see if you don't fall in love yourself.
References
- sample.zip
The sample project for this article - NDoc
The proud subject of this article, on SourceForge - "XML
Comments Let You Build Documentation Directly From Your Visual Studio .NET
Source Files"
J. Andrew Schafer, MSDN Magazine -
"XML Documentation Comments"
C# Programmer's Reference, MSDN Library - XPath Tutorial
Courtesy of W3Schools
Shawn Van Ness is an independent consultant specializing in the development of secure, efficient, and robust distributed software systems, based on .NET, COM, and XML technologies.
Return to ONDotnet.com
- Trackback from http://blog.csdn.net/opteron8309/archive/2005/08/21/460705.aspx
Open Source Reference
2005-08-21 00:46:31 [View]
- Trackback from http://blog.csdn.net/opteron8309/archive/2005/08/20/459625.aspx
单元测试参考
2005-08-19 23:06:28 [View]
-
How to use UseNamespaceDocSummaries
2005-08-08 10:59:59 ppastor [View]
-
How to use UseNamespaceDocSummaries
2005-08-08 11:17:49 ppastor [View]
-
comment tags don't appear in nDoc files
2005-07-30 01:16:45 Jon_Paal [View]
-
comment tags don't appear in nDoc files
2008-06-06 20:37:43 BrcHong [View]
-
comment tags don't appear in nDoc files
2005-07-31 21:31:08 Jon_Paal [View]
- Trackback from http://geekswithblogs.net/mkimber/archive/0001/01/01/14059.aspx
Trackback Message
2004-10-29 13:22:54 [View]
- Trackback from http://geekswithblogs.net/mkimber/archive/2004/10/29/14059.aspx
Trackback Message
2004-10-29 09:42:38 [View]
- Trackback from http://blogs.msdn.com/ericgu/archive/2004/06/25/166046.aspx
Build Comment Web Pages
2004-06-25 09:59:32 [View]
- Trackback from http://weblogs.asp.net/lhunt/articles/5245.aspx
.NET Development Articles
2004-04-08 10:32:29 [View]
- Trackback from http://weblogs.pontonetpt.com/contracorrente/posts/853.aspx
Integra
2004-04-05 10:50:30 [View]
- Trackback from http://weblogs.asp.net/lhunt/articles/5245.aspx
.NET Development Articles
2004-03-10 13:41:07 [View]
- Trackback from http://weblogs.asp.net/lhunt/articles/5245.aspx
.NET Dev Article LinkFest!
2004-03-09 10:14:31 [View]
- Trackback from http://weblogs.asp.net/lhunt/articles/5245.aspx
Essential .NET Articles!
2004-01-09 14:29:49 [View]
- Trackback from http://weblogs.asp.net/lhunt/articles/5245.aspx
Essential .NET Articles!
2004-01-05 10:32:42 [View]
- Trackback from http://weblogs.asp.net/lhunt/articles/5245.aspx
Essential .NET Articles!
2004-01-05 07:56:32 [View]
- Trackback from http://weblogs.asp.net/savanness/archive/0001/01/01/38341.aspx
Let's play...
2004-01-04 20:43:55 [View]
-
Ndoc deletes all files
2004-01-03 06:32:52 anonymous2 [View]
-
Ndoc deletes all files
2005-07-30 01:19:05 Jon_Paal [View]
-
External Documentation
2003-11-25 17:06:01 anonymous2 [View]
-
Performance
2003-07-17 12:14:28 anonymous2 [View]
-
Performance
2003-07-17 12:41:54 arithex [View]
-
multiple dlls
2003-07-16 12:56:20 anonymous2 [View]
-
multiple dlls
2003-07-16 15:01:55 arithex [View]
-
directory structure
2003-07-15 18:48:16 anonymous2 [View]
-
directory structure
2003-07-16 03:06:56 arithex [View]
-
How can i make the help files integrative with the IDE ?
2003-06-16 11:44:03 anonymous2 [View]
-
How can i make the help files integrative with the IDE ?
2003-06-16 14:38:55 arithex [View]
-
XML comments + NDoc for VB.NET !
2002-12-25 10:35:00 anonymous2 [View]
-
XML comments + NDoc for VB.NET !
2003-12-04 11:57:08 anonymous2 [View]
-
XML comments + NDoc for VB.NET !
2003-06-16 14:34:08 arithex [View]
-
Automating NDoc, as part of the build process
2002-12-10 17:27:31 arithex [View]
-
Automating NDoc, as part of the build process
2002-12-13 10:32:49 Shawn Wildermuth |
[View]
-
Automating NDoc, as part of the build process
2003-12-11 01:28:56 anonymous2 [View]

