C# 读取XML注释

时间:2023-03-09 12:55:22
C# 读取XML注释

C#可以通过反射读取类的字段/方法等,可是该如何获取该字段的XML注释?

具体目的:有一个实体类,页面需要有一个与其对应的table,样式大体为

      <tr>
<td>地东经</td>
<td>
<input id='txt_Longitude' type='text' class='form-control' name='Longitude' /></td>
<td>北纬</td>
<td>
<input id='Latitude' type='text' class='form-control' name='Latitude' /></td>
</tr>

其实体类,大致为:

     /// <summary>
/// 东经
/// </summary>
public double? Longitude
{
get{ return _Longitude; }
set
{
this.OnPropertyValueChange(_.Longitude,_Longitude,value);
          this._Longitude=value;
       }
     }

由于实体类中属性很多,生成页面的工作量很大,所以想通过反射的方式,读取实体的XML注释及其属性名称,写个循环即可生成上述页面

后查阅相关资料,XML注释是不写入DLL里的,所以直接通过反射的方式获取XML注释是不可能的.

有建议通过对XML文件进行解析,获取其XML注释的

万能的老外已经处理过类似的问题了,参考这里

其提供了一个处理用的类库以及示例

1.在VS里打开项目属性中的XML文档注释功能

C# 读取XML注释

即在项目的bin目录中生成一个以当前项目命名的xml文件,读取该文件,即可获取XML注释

2.按照如下方式,获取XML注释

XmlElement documentation = DocsByReflection.XMLFromMember(typeof(SomeExampleClass).GetProperty("ExampleProperty"));
Console.WriteLine(documentation["summary"].InnerText.Trim());

下载链接:

docsbyreflection_2

通过反射获取字段名称/字段类型/及其XML注释,即可根据需要生成页面HTML,示例如下:

StringBuilder sb = new StringBuilder();
string rowtemp = "<tr>\r\n" +
"<td>{0}</td>\r\n " +
"<td>" +
"<input id = 'txt_{1}' type = 'text' class='form-control' name='{1}' /></td>\r\n" +
"<td>{2}</td>\r\n" +
"<td>\r\n" +
"<input id = '{3}' type='text' class='form-control' name='{3}' /></td>\r\n" +
"</tr>\r\n"; //遍历基本属性,生成表
PropertyInfo[] ProList = tp1.GetProperties();
List<string> ignoreList = new List<string>();
ignoreList.Add("Project_Code");
bool toEnd = false; #region 区分字段类型
for (int i = 0; i < ProList.Length; )
{
         //下述循环为了实现对字段的过滤,以及一行两个<td>,所以,需要检索出下一个可用的字段对象,看上去比较绕
while (ignoreList.Contains(ProList[i].Name))
{
i++;
if (i == ProList.Length)
{
toEnd = true;
break;
}
}
if (toEnd)
{
break;
}
PropertyInfo p1 = ProList[i];
i++;
PropertyInfo p2 = null;
if (i < ProList.Length)
{
while (ignoreList.Contains(ProList[i].Name))
{
i++;
if (i == ProList.Length)
{
toEnd = true;
break;
}
}
} if (toEnd || i == ProList.Length)
{ }
else
{
p2 = ProList[i];
i++;
}
sb.AppendLine("<tr>");
if (p2 != null)
{
string str1 = getHTML(tp1, p1);
string str2 = getHTML(tp1, p2);
sb.AppendLine(str1);
sb.AppendLine(str2);
}
else
{
string str1 = getHTML(tp1, p1);
sb.AppendLine(str1);
}
sb.AppendLine("</tr>");
}
#endregion write2Text(@"C:\STD\"+tp1.Name+".txt", sb);
Console.WriteLine("结束");
Console.ReadLine();
public static string getCommentText(Type tp1,string nm)
{
Console.WriteLine(nm);
XmlElement documentation = DocsByReflection.XMLFromMember(tp1.GetProperty(nm));
Console.WriteLine(documentation["summary"].InnerText.Trim());
return documentation["summary"].InnerText.Trim();
} public static void write2Text(string filename, StringBuilder sb)
{
using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.Write(sb.ToString());
sw.Flush();
}
}
} public static string getHTML(Type tp1,PropertyInfo p)
{
if (p.Name.Trim() == "Conveyance_Tunnel_Import_Elevation")
Console.WriteLine("");
string inputTD = "<td>{0}</td>\r\n " +
"<td>\r\n" +
"<input id = 'txt_{1}' type = 'text' class='form-control' name='{1}' />\r\n"+
"</td>\r\n"; string selectTD = @"<td>{0}</td>
<td>
<select id='{1}' class='form-control' name='{1}'>
{2}
</select>
</td>"; string chkTD = @"<td>{0}</td>
<td>
<label for='chkY_{1}'>是<input type='checkbox' id='chkY_{1}' value='true' class='form-control' name='{1}'></label>
<label for='chkN_{1}'>否<input type='checkbox' value='false' id='chkN_{1}' class='form-control' name='{1}'></label>
</td>"; if (p.PropertyType == typeof(Guid?)||p.PropertyType==typeof(Guid))
{
//GUID类型的,去数据库中查找并生成Select
StringBuilder optSb = new StringBuilder();
DataTable dt = SqlHelper.ExecuteDataTable("select * from Foreign_Key_Table where Column_Code=@cc and Table_Code=@tc order by Order_ID",
new SqlParameter("@cc", p.Name),new SqlParameter("tc",type_code));
if (dt.Rows.Count > )
{
for (int i = ; i < dt.Rows.Count; i++)
{
optSb.AppendLine(string.Format("<option value='{0}'>{1}</option>", dt.Rows[i]["Key_Code"], dt.Rows[i]["Key_Name"]));
}
return string.Format(selectTD, getCommentText(tp1, p.Name), p.Name, optSb.ToString());
}
else
{
//如果没有查到,非外键表,直接显示
return string.Format(inputTD, getCommentText(tp1, p.Name), p.Name);
}
}
else if (p.PropertyType == typeof(bool?) || p.PropertyType == typeof(bool))
{
return string.Format(chkTD,getCommentText(tp1, p.Name),p.Name);
}
else
{
//生成input
return string.Format(inputTD,getCommentText(tp1,p.Name),p.Name);
}
}