类库说明文档生成器

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


程序界面:


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


关键代码:

/// /// 生成HTML说明文档 /// /// 文件路径 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(@""); htmlBuilder.Append(""); 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(@" ", assembly.InnerText.TrimSpace())); htmlBuilder.Append(string.Format(@" ", 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(@"", methodName, methodDescription)); htmlBuilder.Append(""); 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("", parmsName, parmsType, parmsIsNull, parmsNotes, parmsDefaultValue)); } if (item.SelectNodes("returns").Count > 0) { string retrun = item.SelectNodes("returns")[0].InnerText.TrimSpace(); htmlBuilder.Append(string.Format("", retrun)); } htmlBuilder.Append(""); } } htmlBuilder = new StringBuilder(htmlBuilder.ToString().Substring(0, htmlBuilder.ToString().Length - "".Length)); htmlBuilder.Append("
【命名空间:{0}】
类名:{0}
方法名{0}方法描述{1}
参数名称 参数类型 是否必须 参数描述默认值
{0}{1}{2}{3}{4}
返回结果:{0}
"); htmlBuilder.Append(""); htmlBuilder.Append(@""); string documentFilePath = Environment.CurrentDirectory + Path.DirectorySeparatorChar + "document.htm"; SaveStringToFile(documentFilePath, htmlBuilder.ToString()); ShowFile(documentFilePath); }
源码下载: 下载地址


转载于:https://www.cnblogs.com/zhangqs008/archive/2012/03/22/2498391.html

你可能感兴趣的:(类库说明文档生成器)