从多个XML文档中读取数据用于显示webapi帮助文档
前言:
你先得知道HelpPageConfig文件,不知道说明你现在不需要这个,所以下文就不用看了,等知道了再看也不急.当然如果你很知道这个,下文也不用看了,因为你会了.
方法一:
new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/Documentation.xml"))
替换成
new XmlDocumentationProvider("PluginsFolder/*.xml")
改ctor函数让他支持多个XML文档
using System.Xml.Linq; using System.Xml.XPath; XDocument finalDoc = null; foreach (string file in Directory.GetFiles(@"PluginsFolder", "*.xml")) { if(finalDoc == null) { finalDoc = XDocument.Load(File.OpenRead(file)); } else { XDocument xdocAdditional = XDocument.Load(File.OpenRead(file)); finalDoc.Root.XPathSelectElement("/doc/members") .Add(xdocAdditional.Root.XPathSelectElement("/doc/members").Elements()); } } // Supply the navigator that rest of the XmlDocumentationProvider code looks for _documentNavigator = finalDoc.CreateNavigator();
方法二: 自定义一个支持从目录加载xml文档的XmlDocumentationProvider
public MultiXmlDocumentationProvider(string xmlDocFilesPath) { XDocument finalDoc = null; foreach (string file in Directory.GetFiles(xmlDocFilesPath, "*.xml")) { using (var fileStream = File.OpenRead(file)) { if (finalDoc == null) { finalDoc = XDocument.Load(fileStream); } else { XDocument xdocAdditional = XDocument.Load(fileStream); finalDoc.Root.XPathSelectElement("/doc/members") .Add(xdocAdditional.Root.XPathSelectElement("/doc/members").Elements()); } } } // Supply the navigator that rest of the XmlDocumentationProvider code looks for _documentNavigator = finalDoc.CreateNavigator(); }
使用方法:
config.SetDocumentationProvider(new MultiXmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/")));
方法三:给默认的XmlDocumentationProvider加一个ctor
public XmlDocumentationProvider(IEnumerable<string> documentPaths) { if (documentPaths.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(documentPaths)); } XDocument fullDocument = null; foreach (var documentPath in documentPaths) { if (documentPath == null) { throw new ArgumentNullException(nameof(documentPath)); } if (fullDocument == null) { using (var stream = File.OpenRead(documentPath)) { fullDocument = XDocument.Load(stream); } } else { using (var stream = File.OpenRead(documentPath)) { var additionalDocument = XDocument.Load(stream); fullDocument?.Root?.XPathSelectElement("/doc/members").Add(additionalDocument?.Root?.XPathSelectElement("/doc/members").Elements()); } } } _documentNavigator = fullDocument?.CreateNavigator(); }
使用方法:
var xmlPaths = new[] { HttpContext.Current.Server.MapPath("~/bin/Path.To.FirstNamespace.XML"), HttpContext.Current.Server.MapPath("~/bin/Path.To.OtherNamespace.XML") }; var documentationProvider = new XmlDocumentationProvider(xmlPaths); config.SetDocumentationProvider(documentationProvider);
相关文章:
Web Api Help Page XML comments from more than 1 files(http://stackoverflow.com/questions/22165724/web-api-help-page-xml-comments-from-more-than-1-files/22169357#22169357)
本文地址:
从多个XML文档中读取数据用于显示webapi帮助文档(http://www.cnblogs.com/shiningrise/p/XmlDocumentationProvider.html)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2007-07-14 转:整理收集的div+css制作网页的一些小实例技巧
2007-07-14 ExcelHelper代码