In C#, generally we write the comments for code by using double slash (//). But C# provides the better way to comment our C# code by using XML format. Even we can copy all comments from C# project to make documentation for our project. In this article we learn how to comment our C# code in XML format and document these comments to XML file.
C# Compiler takes the responsibility to convert comments into XML documentation. To document your C# types in XML format, first step is to make use of the new triple slash (///) code comment notations as shown below. To comment C# code in XML format Visual Studio 2008 IDE will generate documentation skeletons by entering triple slash(///) as shown below.
///<summary>
/// This Class Provides methods to access Employee Information
///</summary>
classEmployee
{
///<summary>
/// This method returns Employee Info
///</summary>
///<param name="Id"></param>
///<param name="Name"></param>
///<param name="Sal"></param>
privatevoid EmpInfo(int Id, string Name, decimal Sal)
{
}
}
As shown above, there are different XML elements like <summary>,<param> available to comment the C# Code. Below we can find some of the elements to comment the code.
We can generate XML documentation from these XML elements by using Visual Studio 2008. Right Click on Solution Explorer, select Properties and click on Build tab. In Build tab, under Output section select XML documentation file check box and provide the path for the XML documentation file as shown below.
Build your project and you can find XML documentation file CSharpDocmentationExp.XML in bin\Debug\CSharpDocmentationExp.XML folder and the content of XML file is as shown below.
You can convert this XML file into HTML format manually or by using any third party tool.