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..)

有帮助吗?

解决方案

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.

其他提示

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>

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

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top