Alternative to XInclude

只愿长相守 提交于 2019-12-24 02:43:44

问题


As far as I know there is no support for XInclude in .net.

I'd like to leverage the same kind of mechanism for hierarchically organized XML configuration files. I mean I have a top-level XML config file referencing specific Xml files. My configuration is a cluster of configurations dedicated to one particular module.

How should I do it ? (Or maybe why I shouldn't do it..)


回答1:


First of all, there is some 3rd party support for XInclude in .NET XInclude.NET on Codeplex.

If you are asking because of the configuration files, they have some sort of the same functionality build-in with configSource attribute, see this article describing it.




回答2:


I don't use .net, but you can try using entities...

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



回答3:


MS has created a library for that.

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

should help

  • there are 3ed party solutions.

  • you can extend a XmlReader to read XInclude




回答4:


You can use my Linq to XML XInclude extention method:

/// <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
}

The code was inspired by following blog post: http://catarsa.com/Articles/Blog/Any/Any/Linq-To-Xml-XInclude?MP=pv

Further XInclude infos: http://msdn.microsoft.com/en-us/library/aa302291.aspx



来源:https://stackoverflow.com/questions/14809487/alternative-to-xinclude

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!