Reading data from multiple XML documents is used to display the webapi help document, xmlwebapi
Preface:
You need to know the HelpPageConfig file first. If you do not know it, you do not need to read it now, so you will not need to read it later. of course, if you know this, you don't need to read the following, because you will.
Method 1:
new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/Documentation.xml"))
Replace
new XmlDocumentationProvider("PluginsFolder/*.xml")
Modify the ctor function to support multiple XML documents.
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();
Method 2: customize an XmlDocumentationProvider that supports loading xml documents from a directory
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();}
Usage:
config.SetDocumentationProvider(new MultiXmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/")));
Method 3: Add a ctor to the default XmlDocumentationProvider.
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();}
Usage:
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);
Related Articles:
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)
Address:
Reading data from multiple XML documents for displaying webapi help documentation (http://www.cnblogs.com/shiningrise/p/XmlDocumentationProvider.html)