Search code examples
c#documentationsummary

How to set XML code in NET XML documentation comments


I want to add XML code in -NET C# XML documentation, i found this help: https://stackoverflow.com/a/11030588/2825284 but not work

        /// <summary>
        /// My comentary:
        /// <code>
        ///     <MyXmlCode>
        ///         <XmlNode Atribute="34"/>
        ///     </MyXmlCode>
        /// </code>
        /// </summary>

Solution

  • Your example code contains reserved XML characters that need to be escaped. These characters and their escape sequences are:

    < ... &lt;
    > ... &gt;
    & ... &amp;
    

    So your comment needs to be:

            /// <summary>
            /// My comentary:
            /// <code>
            ///     &lt;MyXmlCode&gt;
            ///         &lt;XmlNode Atribute="34"/&gt;
            ///     &lt;/MyXmlCode&gt;
            /// </code>
            /// </summary>
    

    Or, to make it more readable, use CDATA:

            /// <summary>
            /// My comentary:
            /// <code><![CDATA[
            ///     <MyXmlCode>
            ///         <XmlNode Atribute="34"/>
            ///     </MyXmlCode>
            /// ]]></code>
            /// </summary>
    

    BTW, our VSdocman contains the comment editor which produces CDATA. One drawback of CDATA is that Intellisense doesn't show it currently.