<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tayyab Bin Tariq &#187; Visual Studion .NET</title>
	<atom:link href="http://tayyab.xenoglaux-solutions.com/category/microsoft-technologies/visual-studion-net/feed/" rel="self" type="application/rss+xml" />
	<link>http://tayyab.xenoglaux-solutions.com</link>
	<description>I like sharing what i know</description>
	<lastBuildDate>Fri, 21 May 2010 20:59:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Generating Documentation from XML Comments in C#</title>
		<link>http://tayyab.xenoglaux-solutions.com/2009/06/04/generating-documentation-from-xml-comments-in-c/</link>
		<comments>http://tayyab.xenoglaux-solutions.com/2009/06/04/generating-documentation-from-xml-comments-in-c/#comments</comments>
		<pubDate>Thu, 04 Jun 2009 18:28:35 +0000</pubDate>
		<dc:creator>tayyabtariq</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Visual Studion .NET]]></category>
		<category><![CDATA[Comments]]></category>
		<category><![CDATA[Generate Documentation]]></category>
		<category><![CDATA[Visual Studio.NET]]></category>
		<category><![CDATA[XML Comments]]></category>

		<guid isPermaLink="false">http://tayyab.xenoglaux-solutions.com/2009/06/04/generating-documentation-from-xml-comments-in-c/</guid>
		<description><![CDATA[C# allows developers to embed XML comments into their source files—a useful facility, especially when more than one programmer is working on the same code. The C# parser can expand these XML tags to provide additional information and export them to an external document for further processing. This article shows how to use XML comments [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">C# allows developers to embed XML comments into their source files—a useful facility, especially when more than one programmer is working on the same code. The C# parser can expand these XML tags to provide additional information and export them to an external document for further processing. This article shows how to use XML comments and explains the relevant tags. The author demonstrates how to set up your project to export your XML comments into convenient documentation for the benefit of other developers. He also shows how to use comments to generate help files using <a href="http://sourceforge.net/project/showfiles.php?group_id=36057" target="_blank">nDoc</a>.</p>
<div align="justify"><span id="more-107"></span></div>
<p align="justify">Wouldn&#8217;t it be nice if all this information were kept in one location? The obvious place to put this information is in the source code itself; this makes it easy to modify the docs along with the code. But it is hard enough to go through someone else&#8217;s program when you know how to code, let alone when you are less technically savvy. Also, adding code documentation takes time.   <br />This article will show you how to solve many of those problems using XML comments. Code comments, user manuals, developer manuals, test plans, and many other documents can be generated from a single source using XML tags. I&#8217;ll explain how to insert XML comments and enable exportation of these comments to another file. I&#8217;ll then discuss each of the available tags and the format of the XML file where the tags are used and walk through an example of using XML comments and XSLT to generate help files.</p>
<h2>XML Comments</h2>
<p>All XML comments begin with three forward slashes (///). The first two slashes signify a comment and tell the compiler to ignore the text that follows. The third slash tells the parser that this is an XML comment and should be handled appropriately.   <br />&#160;&#160;&#160;&#160;&#160; When the developer types the three forward slashes, the Microsoft® Visual Studio® .NET IDE checks to see if it precedes an identifiable type or type member definition. If it is identifiable, then the Visual Studio .NET IDE will automatically insert a few comment tags. The developer then adds additional tags and values as needed. For example, here are the XML tags that are generated when the three forward slashes are entered before this member function that has a parameter:</p>
<div>
<pre><code>/// &lt;summary&gt;
///
/// &lt;/summary&gt;
/// &lt;param name=&quot;strFilePath&quot;&gt;&lt;/param&gt;
public void LoadXMLFromFile(string strFilePath)
</code></pre>
</div>
<p align="justify">The tags inserted here represent only a couple of many that the Visual Studio .NET IDE already knows.</p>
<p align="justify">While the current implementation of IntelliSense® for XML comments does not show all the tags listed in the C# specification, the missing tags can always be inserted manually.<br />
  <br />&#160;&#160;&#160;&#160;&#160; Once comments are inserted, it would be useful to do something with them. If you set things up correctly (as I&#8217;ll explain shortly), this usually just means exporting all the text that comes after the three forward slashes into an external file.</p>
<p>&#160;&#160;&#160;&#160;&#160; There are also several predefined XML comment tags that the parser knows. When these tags are encountered (and have been used correctly by the developer), the parser will format the tags&#8217; output to a text file. If a tag that is not predefined is encountered, it is just written verbatim along with any text to the external file. This means that you can make up your own tags or you can even use known tags recognizable from another source (such as HTML) in the comments.</p>
<p>&#160;&#160;&#160;&#160;&#160; When exportation is set up correctly, the C# parser extracts the XML comments, does formatting if needed, and places them into an XML file of your choice. You can set the location and file name of the XML document file, and enable exportation, through your project&#8217;s property pages. To do this, follow these four steps:</p>
<ul>
<li>
<div align="justify">Open the property page for the project, usually by right-clicking on the project in the Solution Explorer, and click Properties.</div>
</li>
<li>
<div align="justify">After the dialog has opened, click the Configuration Properties folder.</div>
</li>
<li>
<div align="justify">Click the Build option.</div>
</li>
<li>
<div align="justify">In the right pane, there will be a property field called XML Documentation File. Set this to the path and file name of the desired file. The path entered is relative to the project directory, not absolute.</div>
</li>
</ul>
<h2>Recognized Tags</h2>
<p align="justify">I have classified the XML comment tags into two categories. The first set of tags, which I&#8217;ll call primary tags, always starts a group of XML comment tags. They are never embedded within other tags. The second set contains tags that are used within primary tags as modifiers to the text. I call them support tags. </p>
<p>here are nine primary tags: &lt;remarks&gt;, &lt;summary&gt;, &lt;example&gt;, &lt;exception&gt;, &lt;param&gt;, &lt;permission&gt;, &lt;returns&gt;, &lt;seealso&gt;, and &lt;include&gt;.<br />
  <br />&#160;&#160;&#160;&#160;&#160; In this context, the &lt;remarks&gt; tag is used to describe a type such as a class: </p>
<div align="justify">
<pre><code>/// &lt;remarks&gt;
/// Class that contains functions to do
/// transformations to help files.
/// &lt;/remarks&gt;
</code></pre>
</div>
<p>&#160;</p>
<p>The C# documentation recommends using &lt;remarks&gt; to describe a type and a &lt;summary&gt; tag to describe a type member.Oddly, if you start a comment before a type using the /// combination, the Visual Studio .NET IDE will still insert a &lt;summary&gt; tag. Therefore, &lt;remarks&gt; tags need to be inserted manually.<br />
  <br />The &lt;summary&gt; tag is the tag found most often in a C# source file. This tag is used to describe type members, including methods, properties, and fields:</p>
<div align="justify">
<pre><code>/// &lt;summary&gt;
/// This XmlDocument based attribute contains the
/// XML documentation for the project.
/// &lt;/summary&gt;
</code></pre>
</div>
<p align="justify">&#160;</p>
<p align="justify">The &lt;summary&gt; tag can describe types as well, but it is not recommended. The XML comment documentation recommends that the &lt;remarks&gt; tag be used for describing types.<br />
  <br />The &lt;example&gt; tag is used to mark the beginning of an example showing how to use the item. The example can be any valid text, but most often it is a snippet of code:</p>
<div align="justify">
<pre><code>/// &lt;example&gt;
/// &lt;code&gt;
/// // create the class that does translations
/// GiveHelpTransforms ght = new GiveHelpTransforms();
/// // have it load our XML into the SourceXML property
/// ght.LoadXMLFromFile(
///  &quot;E:\\Inetpub\\wwwroot\\GiveHelp\\GiveHelpDoc.xml&quot;);
/// &lt;/code&gt;
/// &lt;/example&gt;
</code></pre>
</div>
<p align="justify">&#160;</p>
<p align="justify">If code is used, it is usually marked with a &lt;code&gt; tag. The &lt;code&gt; tag will be discussed in the section on support tags.<br />
  <br />The &lt;exception&gt; tag documents the exceptions, if any, that the item may throw. If more than one exception may be thrown, then more than one of these tags may be used. Unlike most of the tags, the &lt;exception&gt; tag has one attribute, cref. The value of this attribute is the name of the exception that could be thrown. This must be a valid class because the C# parser will verify it and extract the context information to put into the XML documentation. I will explain this later.</p>
<p>I did not throw any exceptions in the code for this article, but here is an example of how the cref attribute can be used with the &lt;exception&gt; tag:</p>
<div align="justify">
<pre><code>/// &lt;exception cref=&quot;SampleException&quot;&gt;
/// Normally, a discussion on any exceptions thrown would go here.
/// &lt;/exception&gt;
</code></pre>
</div>
<p align="justify">&#160;</p>
<p align="justify">The &lt;param&gt; tag describes the parameters of a method or property. It is automatically inserted by the IDE when using the three forward slashes before a method. The &lt;param&gt; tag has one attribute, name, which is simply the same name that the parameter has in the source:</p>
<div align="justify">
<pre><code>/// &lt;param name=&quot;strFilePath&quot;&gt;The path to the file containing the
/// source XML.&lt;/param&gt;
</code></pre>
</div>
<p align="justify">&#160;</p>
<p align="justify">Member access is identified with the &lt;permission&gt; tag. The text that is assigned to it sets permission-related descriptions. There is no requirement for the value for this tag. Permission is one possibility, with values such as public, private, protected, and so on. Scope is another possible value, with information about whether the method is static. However, you are free to put whatever value you must have here.<br />
  <br />The &lt;permission&gt; tag has one attribute—cref. The documentation describes the use of cref in this context as &quot;a reference to a member or field that is available to be called from the current compilation environment.&quot; It is usually set to System.Security.PermissionSet:</p>
<div align="justify">
<pre><code>/// &lt;permission cref=&quot;System.Security.PermissionSet&quot;&gt;Public
/// Access&lt;/permission&gt;
</code></pre>
</div>
<p align="justify">&#160;</p>
<p align="justify">The &lt;returns&gt; tag is similar to the &lt;param&gt; tag, but it&#8217;s used to describe what the method or property returns:</p>
<div align="justify">
<pre><code>/// &lt;returns&gt;The HTML for a list of types based on the XML
/// documentation.&lt;/returns&gt;
</code></pre>
</div>
<p align="justify">&#160;</p>
<p align="justify">The &lt;seealso&gt; tag specifies &quot;other&quot; links related to the same topic. This tag usually does not include a text value, just a cref attribute that specifies a reference to a symbol. This could be a type, member, field, and so on.</p>
<div align="justify">
<pre><code>/// &lt;seealso cref=&quot;GiveMemberListHTMLHelp&quot;/&gt;
</code></pre>
</div>
<p align="justify">&#160;</p>
<p align="justify">The XML compiler will identify the content of the symbol and use it accordingly in the compiled XML documentation. I&#8217;ll discuss this again in the section on the XML documentation file.<br />
  <br />When XML comments are extracted from the code by the compiler, any file specified in the &lt;include&gt; tag will be expanded and the comments inside it will be used as if that had been included inline. Because the compiler finds them, you can keep comments in an external file. This can help organize your code, but then the comments are not readily available. I would never use the &lt;include&gt; tag for this reason, but if your comments are lengthy, you may find the trade-off acceptable.</p>
<p>&#160;&#160;&#160;&#160;&#160; Several attributes must accompany the &lt;include&gt; tag to specify the external file. The file attribute is the name of the file using relative or fully qualified paths. The include file itself is an XML document that holds XML comments. The path attribute is an XPath statement that points to the parent element of the XML comments in the external document, as you can see here:</p>
<div align="justify">
<pre><code>/// &lt;include file='MyXMLCommentFile.xml'
/// path='doc/members/member[@name=&quot;T:MyExampleClass&quot;]/*'/&gt;
public class MyExampleClass
{
/// &lt;include file='MyXMLCommentFile.xml'
   /// path='doc/members/member[@name=&quot;M:MyExampleMethod&quot;]/*'/&gt;
   public string MyExampleMethod(string strReturnThis)
   {
     return strReturnThis;
   }
}
</code></pre>
</div>
<h2>Support Tags</h2>
<p align="justify">&#160;&#160;&#160;&#160;&#160; There are eleven support tags: &lt;c&gt;, &lt;code&gt;, &lt;list&gt;, &lt;listheader&gt;, &lt;item&gt;, &lt;term&gt;, &lt;description&gt;, &lt;para&gt;, &lt;paramref&gt;, &lt;see&gt;, and &lt;value&gt;.<br />
  <br />&#160;&#160;&#160;&#160;&#160; The &lt;c&gt; tag marks a line of text as code. It is usually used inline in descriptive text.</p>
<div align="justify">
<pre><code>/// The source XML is loaded into the &lt;see cref=&quot;SourceXML&quot;/&gt;
/// property (e.g. &lt;c&gt;&lt;I&gt;obj&lt;/I&gt;.SourceXML =
/// &quot;&lt;I&gt;XML goes here&lt;/I&gt;&quot;&lt;/c&gt;).
</code></pre>
</div>
<p align="justify">&#160;</p>
<p align="justify">The &lt;code&gt; tag also defines a section of text as code. It is often used within an &lt;example&gt; tag block (as shown earlier). The &lt;code&gt; tag is similar to the &lt;c&gt; tag, but &lt;c&gt; is used for a single line of code while &lt;code&gt; is used for a block of code. It specifies that the formatting of the text in the comments needs to remain the same:</p>
<div align="justify">
<pre><code>/// &lt;code&gt;
/// // create the class that does translations
/// GiveHelpTransforms ght = new GiveHelpTransforms();
/// // have it load our XML into the SourceXML property
/// ght.LoadXMLFromFile(
///      &quot;E:\\Inetpub\\wwwroot\\GiveHelp\\GiveHelpDoc.xml&quot;);
/// &lt;/code&gt;
</code></pre>
</div>
<p align="justify">&#160;</p>
<p align="justify">The &lt;list&gt; tag is used in other comment tags to define a specialized list. The type of list is defined by the type attribute, which can have the values bullet, number, or table. Within the &lt;list&gt; tag, there are tags that denote the components of the list, as you can see in this example:</p>
<div align="justify">
<pre><code>/// &lt;list type=&quot;table&quot;&gt;
/// &lt;listheader&gt;
/// &lt;term&gt;Help Page&lt;/term&gt;
/// &lt;description&gt;Function to call&lt;/description&gt;
/// &lt;/listheader&gt;
/// &lt;item&gt;&lt;term&gt;List of Types&lt;/term&gt;
/// &lt;description&gt;GiveTypeListHTMLHelp&lt;/description&gt;&lt;/item&gt;
/// &lt;item&gt;&lt;term&gt;List of members&lt;/term&gt;
/// &lt;description&gt;GiveMemberListHTMLHelp&lt;/description&gt;&lt;/item&gt;
/// &lt;item&gt;&lt;term&gt;Help for a single member&lt;/term&gt;
/// &lt;description&gt;GiveMemberHTMLHelp&lt;/description&gt;&lt;/item&gt;
/// &lt;/list&gt;
</code></pre>
</div>
<p align="justify">&#160;</p>
<p align="justify">The &lt;listheader&gt; tag holds header information for the list (see the previous example). It is typically used for a table of list types.<br />
  <br />As you probably guessed, the &lt;item&gt; tag identifies each item in the list. It can stand alone or wrap &lt;term&gt; and &lt;description&gt; tags. The &lt;term&gt; and &lt;description&gt; tags are always children tags of the &lt;listheader&gt; tag or &lt;item&gt; tags. They are always used in pairs and their function is obvious.</p>
<p>The &lt;para&gt; tag is used to identify a new paragraph. It is very similar to the &lt;P&gt; tag in HTML. You should definitely use this tag to break up long comment sections:</p>
<div align="justify">
<pre><code>/// &lt;summary&gt;This is a summary.
/// &lt;para&gt;This is a new paragraph.&lt;/para&gt;
/// &lt;/summary&gt;
</code></pre>
</div>
<p align="justify">&#160;</p>
<p align="justify">If text within a comment needs to have a parameter specially identified, then &lt;paramref&gt; is the tag to use. It is inserted in the text in the location where the parameter text should appear. The main purpose of this tag is to identify the name of a parameter that should be formatted in a special way. It has one attribute, name, which is the name of the parameter that would appear in the text, usually formatted in a special way:</p>
<div align="justify">
<pre><code>/// Loads the XML documentation in the file specified by
/// &lt;paramref&gt;strFilePath&lt;/paramref&gt;.
</code></pre>
</div>
<p align="justify">&#160;</p>
<p align="justify">The &lt;see&gt; tag is used within the text of other comment tags to specify a hyperlink. It is used inline as part of the text and usually just includes one attribute, cref:</p>
<div align="justify">
<pre><code>/// One of the associated member functions (&lt;see
/// cref=&quot;GiveTypeListHTMLHelp&quot;/&gt;,
/// &lt;see cref=&quot;GiveMemberListHTMLHelp&quot;/&gt;, &lt;see
/// cref=&quot;GiveMemberHTMLHelp&quot;/&gt;)
/// is called to initiate and then return the transformation.
</code></pre>
</div>
<p align="justify">&#160;</p>
<p align="justify">The cref attribute specifies a reference to an existing symbol. See the description for the &lt;exception&gt; tag for more information.<br />
  <br />The &lt;value&gt; tag defines the meaning of a property member. It is used just like the &lt;remarks&gt; tag is used for classes and the &lt;summary&gt; tag is used for other members.</p>
<div align="justify">
<pre><code>/// &lt;value&gt;
/// The SourceXML property contains the XML that will be used in
/// the transformations by the member functions for this class.
/// &lt;/value&gt;</code></pre>
</div>
<div align="justify">
<pre><code></code></pre>
</div>
<h2 align="justify"><code><font face="Lucida Sans Unicode">Generating Documentation</font></code></h2>
<p align="justify"><code></code></p>
<p align="justify"><code></code></p>
<div align="right">
<pre><code><font face="Lucida Sans Unicode">Now that you have successfully created an XML file from the comments you need to generate documentation from it. For this purpose you can download and use </font><a href="http://sourceforge.net/project/showfiles.php?group_id=36057" target="_blank"><font face="Lucida Sans Unicode">nDoc</font></a><font face="Lucida Sans Unicode">.</font></code></pre>
</div>
<div align="right">
<pre><code><font face="Lucida Sans Unicode">Once you have downloaded nDoc, you can use its GUI interface to select a binary from your project directory (make sure it is in the debug folder and the .XML is located next to it).</font></code></pre>
</div>
<div align="right">
<pre><code><font face="Lucida Sans Unicode">nDoc allows you to create different types of documentations from simple HTML to MSDN style.</font></code></pre>
</div>
<div align="right">
<pre><code><font face="Lucida Sans Unicode"></font></code></pre>
</div>
<div align="right">
<pre><code><font face="Lucida Sans Unicode">If you need anymore help, please feel free to ask.</font></code></pre>
</div>
<div align="right">
<pre><code><font face="Lucida Sans Unicode">Related Article;</font></code></pre>
</div>
<div align="right">
<pre><a title="http://msdn.microsoft.com/en-us/magazine/cc302121.aspx" href="http://msdn.microsoft.com/en-us/magazine/cc302121.aspx"><font face="Lucida Sans Unicode">http://msdn.microsoft.com/en-us/magazine/cc302121.aspx</font></a></pre>
</div>
<p class="facebook"><a href="http://www.facebook.com/share.php?u=http://tayyab.xenoglaux-solutions.com/2009/06/04/generating-documentation-from-xml-comments-in-c/" target="_blank"><img src="http://tayyab.xenoglaux-solutions.com/wp-content/plugins/add-to-facebook-plugin/facebook_share_icon.gif" alt="Share on Facebook" title="Share on Facebook" /></a><a href="http://www.facebook.com/share.php?u=http://tayyab.xenoglaux-solutions.com/2009/06/04/generating-documentation-from-xml-comments-in-c/" target="_blank" title="Share on Facebook">Share on Facebook</a></p>]]></content:encoded>
			<wfw:commentRss>http://tayyab.xenoglaux-solutions.com/2009/06/04/generating-documentation-from-xml-comments-in-c/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

