类库说明文档生成器

 原理很简单,解析程序集生成时的xml描述文档。设置生成程序集时生成xml描述文档的操作如下:

类库说明文档生成器

程序界面:

类库说明文档生成器

生成后的文档视图(样式调整需改源码)

类库说明文档生成器

关键代码:

        /// <summary>
        /// 生成HTML说明文档
        /// </summary>
        /// <param name="filePath">文件路径</param>
        public void GenneralHtmlDocument(string filePath)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(filePath);
            XmlElement root = doc.DocumentElement;
            XmlNodeList nodeList = root.SelectNodes("members")[0].SelectNodes("member");
            XmlNode assembly = root.SelectNodes("assembly")[0].SelectNodes("name")[0];
            StringBuilder htmlBuilder = new StringBuilder();
            htmlBuilder.Append(@"<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.0 Transitional//EN'><html><head><title></title></head><body>");
            htmlBuilder.Append("<table>");
            foreach (XmlNode item in nodeList)
            {
                string name = item.Attributes["name"].InnerText;
                if (name.StartsWith("T"))//类标识
                {
                    string className = name.Substring(name.LastIndexOf(".") + 1);
                    htmlBuilder.Append(string.Format(@" <tr><td colspan='6'> <b>【命名空间:{0}】</b></td></tr>", assembly.InnerText.TrimSpace()));
                    htmlBuilder.Append(string.Format(@" <tr><td colspan='6'> <b>类名:{0}</b></td></tr>", className));
                }
                else if (name.StartsWith("M"))//方法标识
                {
                    string parmsTypeString = string.Empty;
                    string[] parmsTypeArr = new string[] { };
                    if (name.Contains("(") & name.Contains(")"))
                    {
                        parmsTypeString = name.Substring(name.IndexOf("(") + 1, name.LastIndexOf(")") - (name.IndexOf("(") + 1));
                        parmsTypeArr = parmsTypeString.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    }
                    name = name.Contains("(") ? name.Substring(0, name.LastIndexOf("(")) : name;
                    string methodName = name.Substring(name.LastIndexOf(".") + 1);
                    string methodDescription = item.SelectNodes("summary")[0].InnerText.TrimSpace();
                    htmlBuilder.Append(string.Format(@"<tr><td class='tdleft'>方法名</td><td>{0}</td><td class='tdleft'>方法描述</td><td colspan='2'>{1}</td></tr>", methodName, methodDescription));
                    htmlBuilder.Append("<tr> <td class='tdleft'>参数名称 </td> <td class='tdleft'> 参数类型 </td> <td class='tdleft'> 是否必须 </td><td class='tdleft'>参数描述</td><td class='tdleft'>默认值</td> </tr>");

                    XmlNodeList parmsList = item.SelectNodes("param");
                    for (int i = 0; i < parmsList.Count; i++)
                    {
                        string parmsName = parmsList[i].Attributes["name"].InnerText.TrimSpace();
                        string parmsType = i < parmsTypeArr.Length ? parmsTypeArr[i].TrimSpace() : "";
                        string parmsNotes = parmsList[i].InnerText.TrimSpace();
                        string parmsIsNull = "Null";
                        string parmsDefaultValue = GetTypeDefaultValue(parmsType);
                        htmlBuilder.Append(string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td></tr>",
                              parmsName, parmsType, parmsIsNull, parmsNotes, parmsDefaultValue));
                    }
                    if (item.SelectNodes("returns").Count > 0)
                    {
                        string retrun = item.SelectNodes("returns")[0].InnerText.TrimSpace();
                        htmlBuilder.Append(string.Format("<tr><td  class='tdleft'>返回结果:</td><td colspan='5'>{0}</td></tr>", retrun));
                    }
                    htmlBuilder.Append("<tr><td colspan='6'></td></tr>");
                }
            }
            htmlBuilder = new StringBuilder(htmlBuilder.ToString().Substring(0, htmlBuilder.ToString().Length - "<tr><td colspan='6'></td></tr>".Length));
            htmlBuilder.Append("</table>");
            htmlBuilder.Append("<style type='text/css'>");
            htmlBuilder.Append(".tdleft{  background-color: #F2F5A9;width:10%;}");
            htmlBuilder.Append(".tdvalue{ width:200px;}");
            //表格加边框样式
            htmlBuilder.Append(@"table{border-collapse:collapse;border-spacing:0;border-left:1px solid #888;border-top:1px solid #888;background:#efefef;}th,td{border-right:1px solid #888;border-bottom:1px solid #888;padding:5px 15px;}th{font-weight:bold;background:#ccc;}");
            htmlBuilder.Append("</style>");
            htmlBuilder.Append(@"</body></html>");
            string documentFilePath = Environment.CurrentDirectory + Path.DirectorySeparatorChar + "document.htm";
            SaveStringToFile(documentFilePath, htmlBuilder.ToString());
            ShowFile(documentFilePath);
        }

源码下载:下载地址


上一篇:Qt开源作品12-硬盘容量控件


下一篇:Qt开源作品20-PNG图片警告去除工具