Exporting Comments In Visual Studio

Introduction

While I was searching on the internet about comments, I found out about this feature. This feature helps us to know the comments without opening Visual Studio. In modern life, we aren't able to spend more time preparing a project document. Instead of preparing the document, we can use these comment for quick reference.

Exporting comments

Step 1

I have created a sample project with a comment on the top of every method that is available in the class (Refer to the below code).

Program.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using Operstion;  
  6. namespace ConsoleApplication {  
  7.     class Program {  
  8.         static void Main(string[] args) {  
  9.             Operation();  
  10.         }  
  11.         /// <summary>  
  12.         /// method to perform operation  
  13.         /// </summary>  
  14.         public static void Operation() {  
  15.             Operstion.Operation op = new Operstion.Operation();  
  16.             Console.WriteLine("select the operation");  
  17.             Console.WriteLine("\n 1. Additon");  
  18.             Console.WriteLine("\n 2. Subtraction");  
  19.             Console.WriteLine("\n 3. Division");  
  20.             Console.WriteLine("\n 4. Multiplication");  
  21.             int operationtype = int.Parse(Console.ReadLine().ToString());  
  22.             Console.WriteLine("Enter the value A: ");  
  23.             int a = int.Parse(Console.ReadLine().ToString());  
  24.             Console.WriteLine("Enter the value B: ");  
  25.             int b = int.Parse(Console.ReadLine().ToString());  
  26.             int c = 0;  
  27.             switch (operationtype) {  
  28.                 case 1:  
  29.                     DisplayResult(op.Addition(a, b));  
  30.                     break;  
  31.                 case 2:  
  32.                     DisplayResult(op.Subtraction(a, b));  
  33.                     break;  
  34.                 case 3:  
  35.                     DisplayResult(op.Division(a, b));  
  36.                     break;  
  37.                 case 4:  
  38.                     DisplayResult(op.Multiplication(a, b));  
  39.                     break;  
  40.             }  
  41.         }  
  42.         /// <summary>  
  43.         /// Method to display the result from the user selected operation  
  44.         /// </summary>  
  45.         /// <param name="result"></param>  
  46.         public static void DisplayResult(int result) {  
  47.             Console.WriteLine("Result of A and B is :" + result);  
  48.             Console.ReadKey();  
  49.         }  
  50.     }  
  51. }  

Operastion.Operation.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace Operstion {  
  6.     public class Operation {  
  7.         /// <summary>  
  8.         /// Method to perform addition  
  9.         /// </summary>  
  10.         /// <param name="a"></param>  
  11.         /// <param name="b"></param>  
  12.         /// <returns></returns>  
  13.         public int Addition(int a, int b) {  
  14.             return a + b;  
  15.         }  
  16.         /// <summary>  
  17.         /// Method to perform subtraction  
  18.         /// </summary>  
  19.         /// <param name="a"></param>  
  20.         /// <param name="b"></param>  
  21.         /// <returns></returns>  
  22.         public int Subtraction(int a, int b) {  
  23.             return a - b;  
  24.         }  
  25.         /// <summary>  
  26.         /// method to perform division  
  27.         /// </summary>  
  28.         /// <param name="a"></param>  
  29.         /// <param name="b"></param>  
  30.         /// <returns></returns>  
  31.         public int Division(int a, int b) {  
  32.             return a / b;  
  33.         }  
  34.         /// <summary>  
  35.         /// method to perform multiplication  
  36.         /// </summary>  
  37.         /// <param name="a"></param>  
  38.         /// <param name="b"></param>  
  39.         /// <returns></returns>  
  40.         public int Multiplication(int a, int b) {  
  41.             return a * b;  
  42.         }  
  43.     }  
  44. }  

Step 2

Now go to project -> ConsoleApplication properties…. and select (Name of the project like below).

Visual Studio

Step 3

Now consoleApplication properties will display like the below image. In that, select Build option and then select XML documentation file checkbox and specify the XML file location.

Visual Studio

 

The comments which we have specified in the project have been converted into XML and will generate into XML files on the specified XML path.

In my project, there are two projects - one is a console application project and another one is an operation class project. While building the project, the XML will be generated. (I have also modified the project option for the operation class to generate XML file. We need to enable XML document option for the project that needs comments XML).

Step 4

Now, build the console application and the go the path that is specified to generate the XML file. We will be able to find two XML files with the project name.

Visual Studio

 

When we open these files, we are able to find the comments in the project.

ConsoleApplication.XML

  1. <?xml version="1.0"?>  
  2. <doc>  
  3.     <assembly>  
  4.         <name>ConsoleApplication</name>  
  5.     </assembly>  
  6.     <members>  
  7.         <member name="M:ConsoleApplication.Program.Operation">  
  8.             <summary> method to perform operation </summary>  
  9.         </member>  
  10.         <member name="M:ConsoleApplication.Program.DisplayResult(System.Int32)">  
  11.             <summary> Method to display the result from the user selected operation </summary>  
  12.             <param name="result">  
  13.             </param>  
  14.         </member>  
  15.     </members>  
  16. </doc>  

Operstiond.xml

  1. <?xml version="1.0"?>  
  2. <doc>  
  3.     <assembly>  
  4.         <name>Operstiond</name>  
  5.     </assembly>  
  6.     <members>  
  7.         <member name="M:Operstion.Operation.Addition(System.Int32,System.Int32)">  
  8.             <summary> Method to perform addition </summary>  
  9.             <param name="a">  
  10.             </param>  
  11.             <param name="b">  
  12.             </param>  
  13.             <returns></returns>  
  14.         </member>  
  15.         <member name="M:Operstion.Operation.Subtraction(System.Int32,System.Int32)">  
  16.             <summary> Method to perform subtraction </summary>  
  17.             <param name="a">  
  18.             </param>  
  19.             <param name="b">  
  20.             </param>  
  21.             <returns></returns>  
  22.         </member>  
  23.         <member name="M:Operstion.Operation.Division(System.Int32,System.Int32)">  
  24.             <summary> method to perform division </summary>  
  25.             <param name="a">  
  26.             </param>  
  27.             <param name="b">  
  28.             </param>  
  29.             <returns></returns>  
  30.         </member>  
  31.         <member name="M:Operstion.Operation.Multiplication(System.Int32,System.Int32)">  
  32.             <summary> method to perform multiplication </summary>  
  33.             <param name="a">  
  34.             </param>  
  35.             <param name="b">  
  36.             </param>  
  37.             <returns></returns>  
  38.         </member>  
  39.     </members>  
  40. </doc>  

The above screenshot shows the comments which we have given in the project have been extracted in the XML with the tags.

Conclusion

Thus, Visual Studio makes us generate given comments as document and makes the developer's work much easier. Also, there are so many tools available in the market to generate comments as a document.