This article shows how to create documentation in C# in a simple and fast way. Like my previous articles, this one too focuses beginner to intermediate level. Advance developers please scroll to the bottom of this article. Remember the days when programmers used to work hard to do the documentation? Trust me, it's a huge difference nowadays. Now, all a programmer need to do is to embed the XML documentation right into the code and let Visual Studio .NET perform the rest.
Note: If you are very new to XML, click here to checkout my previous article on XML walkthrough.
To start with, just follow the instructions step by step.
Step By Step Walkthrough:
- Go to File->New->Blank Solution.
- Select Visual C# Projects in Project Types.
- Select ClassLibrary in Templates.
- Type, for e.g. TestXMLdoc, in the Name textbox.
Creating a simple class XML file (the class which will have the comments which will be read by our Visual Studio .NET to create the XML documentation)
Replace the code in the class with the following:
Compiling your application after specifying the documentation filename
Note: If you are not using the Visual Studio .NET IDE, you should go for the first one from the following optionss:
- At the compile time, you just need to specify the file name like the following:
csc testXMLdoc.cs /doc:testXMLdoc.xml
- If you are using the powerful Visual Studio .NET IDE, then follow these steps:
- Go to to the Solution Explorer.
- Right click the project and go to Properties.
- This will open a dialog box.
- Locate the configuration properties -> Build
- Specify the output path, and XML documentation file name, say TestXMLdoc. That's it.
- Compile it and it will create the file at the specified location.
Unlike Java, Microsoft Visual C# generates XML documentation instead of HTML, and as we know about XML, it's mould-able and later can be used anywhere else we need. The following is a list of tags along with their description which can be used in creation of XML documentation.
This will textually include provided code in a single line.
E.g.: <c> int i = 0 ; </c>
This will textually include provided code in multiple lines (snippet).
This will include a code example.
This will force the compiler whether it matches a possible exception.
This will fetch the documentation from an external file. Same as
#include in scripting languages.
This will insert a list into the documentation file.
This specifies the parameters of the method and makes the compiler verify it.
This will mark a parameter.
This will determine the access permissions.
This will include a descriptive text.
This will mark the return value of a member.
It is a reference to a related item.
This is similar to the 'See also' section of the MSDN help.
A summary of the member item. Normally, Visual Studio .NET prepares this automatically along with its close tag the moment you type.
- /// (three slashes) in its editor.
This is nothing but a property.
Opening the XML documentation file
- Go to the specified path (by default, your application's bin/Debug).
- Open the XML file (in our case, TestXMLdoc.xml) in Internet Explorer.
- Check out: it should look like the following:
Creating an HTML document file automatically
Wasn't that fun? It was.. I know. But that was just a trailer, let's see the whole movie.
- Go to menu Tools->Build Comment Web Pages.
- On the dialog box, click OK to see the magic (you may change the options as per your need but since we have a single class, let's leave it to default ones).
- Click on the name of the solution on the created web page.
- Now, click on the project name in the displayed tree.
- And ...by now, you must be curious and must have already clicked the class name within.
That's the result of your patience, hard work and cooperation.
Note: The above example was for people who need a tool to suffice their minimum requirements. For advance documentation, you would like to opt for a more sophisticated tool like NDoc Code Documentation Generator for .NET.
NDoc generates class library documentation from .NET assemblies and the XML documentation files generated by the C# compiler (or with an add-on tool for VB.NET). And the output looks like the following:
NDoc uses pluggable documenters to generate documentation in several different formats, including the MSDN-style HTML Help format (.chm), the Visual Studio .NET Help format (HTML Help 2), and MSDN-online style web pages.
You can download the latest version: NDOC 1.3 Beta or NDOC 1.2.
Thanks to Ms. Manisha Mahajani for reviewing the article for errors.