Hi @mc , Welcome to Microsoft Q&A,
In Visual Studio, you need to make sure Generate XML documentation comments is enabled and that the XML file is in the correct output directory.
Please follow these steps to check and set XML document comments:
Enable generating XML documentation comments in the project properties. In Visual Studio, right-click the project and select Properties. Go to the "Build" tab. Make sure "XML Document File" is checked.
using System;
namespace xxx
{
internal class Program
{
public static void Main(string[] args)
{
Type type = typeof(MyClassA);
var classSummary = GetClassSummary(type);
var idSummary = GetMemberSummary(type, "Id");
Console.WriteLine("Class summary:");
Console.WriteLine(classSummary);
Console.WriteLine("\nId property summary:");
Console.WriteLine(idSummary);
Console.ReadLine();
}
public static string GetClassSummary(Type type)
{
var assembly = type.Assembly;
var xmlFilePath = $"{assembly.GetName().Name}.xml";
var xmlDocumentation = new System.Xml.XmlDocument();
xmlDocumentation.Load(xmlFilePath);
var classNode = xmlDocumentation.SelectSingleNode($"/doc/members/member[starts-with(@name, 'T:{type.FullName}')]");
if (classNode != null)
{
var summaryNode = classNode.SelectSingleNode("summary");
if (summaryNode != null)
{
return summaryNode.InnerText.Trim();
}
}
return "Summary not found.";
}
public static string GetMemberSummary(Type type, string memberName)
{
var assembly = type.Assembly;
var xmlFilePath = $"{assembly.GetName().Name}.xml";
var xmlDocumentation = new System.Xml.XmlDocument();
xmlDocumentation.Load(xmlFilePath);
var memberNode = xmlDocumentation.SelectSingleNode($"/doc/members/member[starts-with(@name, 'P:{type.FullName}.{memberName}')]");
if (memberNode != null)
{
var summaryNode = memberNode.SelectSingleNode("summary");
if (summaryNode != null)
{
return summaryNode.InnerText.Trim();
}
}
return "Summary not found.";
}
}
/// <summary>
/// This is my MyClassA
/// </summary>
public class MyClassA
{
/// <summary>
/// Id property
/// </summary>
public int Id { get; set; }
}
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.