zoukankan      html  css  js  c++  java
  • 从多个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)

  • 相关阅读:
    引物设计重复性查看
    NCBI获取指定区域基因序列及其引物设计
    视频录制软件——Bandicam使用介绍
    SX知识学习——IGV
    常用网站总结
    计算机知识学习——window上配置perl环境(转)
    ubuntu virtualenv安装
    Windows下使用virtualenv
    linux ssh连接设置
    linux 搭建FTP
  • 原文地址:https://www.cnblogs.com/shiningrise/p/XmlDocumentationProvider.html
Copyright © 2011-2022 走看看