3

据我所知XInclude,.net 中不支持。

我想为分层组织的 XML 配置文件利用相同的机制。我的意思是我有一个引用特定 Xml 文件的顶级 XML 配置文件。我的配置是一组专用于一个特定模块的配置。

我应该怎么做?(或者也许为什么我不应该这样做..)

4

4 回答 4

2

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

<!DOCTYPE example [
<!ENTITY doc1 SYSTEM "doc1.xml">
<!ENTITY doc2 SYSTEM "doc2.xml">
]>
<example>
&doc1;
&doc2;
</example>
于 2013-02-11T09:47:29.150 回答
2

首先,在 Codeplex 上的 .NET XInclude.NET中有一些对 XInclude 的第 3 方支持。

如果您因为配置文件而询问,它们具有某种与 configSource 属性相同的内置功能,请参阅描述它的这篇文章。

于 2013-02-11T09:48:56.763 回答
1

MS为此创建了一个库。

查看http://www.microsoft.com/en-us/download/details.aspx?id=4972

应该有帮助

  • 有 3ed 方解决方案。

  • 你可以扩展一个 XmlReader 来读取 XInclude

于 2013-02-11T09:47:55.337 回答
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

于 2013-12-09T15:09:11.277 回答