替代XInclude的方案

4
据我所知,.net不支持XInclude。
我想利用同样的机制来处理分层组织的XML配置文件。我的意思是有一个顶级XML配置文件引用特定的XML文件。我的配置是一组专门用于一个特定模块的配置。
应该如何处理?(或者为什么不应该这样做...)
4个回答

2

我不使用.net,但你可以尝试使用实体...

<!DOCTYPE example [
<!ENTITY doc1 SYSTEM "doc1.xml">
<!ENTITY doc2 SYSTEM "doc2.xml">
]>
<example>
&doc1;
&doc2;
</example>

请参阅http://msdn.microsoft.com/en-us/library/aa302291.aspx,其中有一个特殊的部分解释为什么应该优先选择XInclude而不是实体。 - George Birbilis

2
首先,在.NET中有一些第三方支持XInclude的工具,可以参考Codeplex上的XInclude.NET
如果您是因为配置文件而询问此问题,那么它们具有与configSource属性相同的某种功能内置,可以参考这篇文章进行了描述。

似乎 MVP.XML 包与 MS 提供的 XInclude.net 具有相同的类名(XIncludingReader)。可能它们共享相同的代码。以下是链接供参考。 - George Birbilis
看起来他们有一个比MS下载站提供的XIncludingReader更新一点的版本,该版本是从2007年发布的,相较于2004年发布的版本,它具有更多的功能。这些信息可以在https://mvpxml.codeplex.com/releases/view/4894上的注释中得到证实。 - George Birbilis
NuGet上唯一另一个具有“XInclude”标签的软件包似乎是这个:https://www.nuget.org/packages/NXKit.XInclude/ - George Birbilis
http://web.archive.org/web/20170821171150/http://mvpxml.codeplex.com/wikipage?title=XInclude.NET - Tom

1
你可以使用我的 Linq to XML XInclude 扩展方法:
/// <summary>
/// Linq to XML XInclude extentions
/// </summary>
public static class XIncludeExtention
{
    #region fields

    /// <summary>
    /// W3C XInclude standard
    /// Be aware of the different 2001 and 2003 standard.
    /// </summary>
    public static readonly XNamespace IncludeNamespace = "http://www.w3.org/2003/XInclude";

    /// <summary>
    /// Include element name
    /// </summary>
    public static readonly XName IncludeElementName = IncludeNamespace + "include";

    /// <summary>
    /// Include location attribute
    /// </summary>
    public const string IncludeLocationAttributeName = "href";

    /// <summary>
    /// Defines the maximum sub include count of 25
    /// </summary>
    public const int MaxSubIncludeCountDefault = 25;

    #endregion


    #region methods


    /// <summary>
    /// Replaces XInclude references with the target content.
    /// W3C Standard: http://www.w3.org/2003/XInclude
    /// </summary>
    /// <param name="xDoc">The xml doc.</param>
    /// <param name="maxSubIncludeCount">The max. allowed nested xml includes (default: 25).</param>
    public static void ReplaceXIncludes(this XDocument xDoc, int maxSubIncludeCount = MaxSubIncludeCountDefault)
    {
        ReplaceXIncludes(xDoc.Root, maxSubIncludeCount);
    }

    /// <summary>
    /// Replaces XInclude references with the target content.
    /// W3C Standard: http://www.w3.org/2003/XInclude
    /// </summary>
    /// <param name="xmlElement">The XML element.</param>
    /// <param name="maxSubIncludeCount">The max. allowed nested xml includes (default: 25).</param>
    public static void ReplaceXIncludes(this XElement xmlElement, int maxSubIncludeCount = MaxSubIncludeCountDefault)
    {
        xmlElement.ReplaceXIncludes(1, maxSubIncludeCount);
    }

    private static void ReplaceXIncludes(this XElement xmlElement, int subIncludeCount, int maxSubIncludeCount)
    {
        var results = xmlElement.DescendantsAndSelf(IncludeElementName).ToArray<XElement>();    // must be materialized

        foreach (var includeElement in results)
        {
            var path = includeElement.Attribute(IncludeLocationAttributeName).Value;
            path = Path.GetFullPath(path);

            var doc = XDocument.Load(path);
            if (subIncludeCount <= maxSubIncludeCount)  // protect mutal endless references
            {
                // replace nested includes
                doc.Root.ReplaceXIncludes(++subIncludeCount, maxSubIncludeCount);
            }
            includeElement.ReplaceWith(doc.Root);
        }
    }

    #endregion
}

这段代码的灵感来自以下博客文章:http://catarsa.com/Articles/Blog/Any/Any/Linq-To-Xml-XInclude?MP=pv 更多关于XInclude的信息:http://msdn.microsoft.com/en-us/library/aa302291.aspx

1

顺便说一下,原作者(tkachenko)还有一个支持XInclude的XSL转换工具(NXSLT-http://www.tkachenko.com/blog/archives/000646.html),但似乎下载站点不再存在。我将研究在我的XSLTranform工具(http://zoomicon.com/tranXform)中添加XInclude支持,因为它是用.NET制作的。此外,他们提供了XIncludingReader,您可以在管道中使用它来代替XmlReader或其他地方的包装器。 - George Birbilis
顺便提一下,这个下载似乎是从2004年的,而包含相同代码的Mvp.Xml(https://mvpxml.codeplex.com/wikipage?title=XInclude.NET)库是从2007年的 - https://mvpxml.codeplex.com/releases/view/4894,并且有一些更新的内容(例如,它说XIncludingReader现在实现了IXmlLineInfo接口等)。 - George Birbilis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接