Reading data from multiple XML documents is used to display the webapi help document, xmlwebapi

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.