4

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

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
LB40
  • 12,041
  • 17
  • 72
  • 107

4 Answers4

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>
Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
  • see http://msdn.microsoft.com/en-us/library/aa302291.aspx, there is a special section on why one should prefer XInclude to entities – George Birbilis Sep 25 '14 at 13:35
2

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.

jhexp
  • 685
  • 4
  • 9
  • seems the MVP.XML package has same class name (XIncludingReader) with the XInclude.net that MS ships (see below for links). Maybe they share the same code – George Birbilis Sep 25 '14 at 13:17
  • seem they have a bit newer version, from 2007 (than the one MS downloads site ships, from 2004) of XIncludingReader with more features for the XIncludingReader, judging from notes at https://mvpxml.codeplex.com/releases/view/4894 – George Birbilis Sep 25 '14 at 13:33
  • the only other package on NuGet that has "XInclude" Tag seems to be this one: https://www.nuget.org/packages/NXKit.XInclude/ – George Birbilis Sep 25 '14 at 13:50
  • http://web.archive.org/web/20170821171150/http://mvpxml.codeplex.com/wikipage?title=XInclude.NET – Jan Jun 14 '22 at 09:17
1

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

Tomer W
  • 3,395
  • 2
  • 29
  • 44
  • the idea is that they provide XIncludingReader you can use instead of XmlReader or in other places in your pipeline as a wrapper btw, the original author (tkachenko) also had an XSL transformation tool that supported XInclude (NXSLT - http://www.tkachenko.com/blog/archives/000646.html), but seems the download site is not existing anymore. I will look into adding XInclude support to my XSLTranform tool (http://zoomicon.com/tranXform) since it is made with .NET – George Birbilis Sep 25 '14 at 13:21
  • btw, this download seems to be from 2004, whereas the Mvp.Xml (https://mvpxml.codeplex.com/wikipage?title=XInclude.NET) library that includes the same code is from 2007 - https://mvpxml.codeplex.com/releases/view/4894 and has some newer stuff (e.g. it says XIncludingReader now implements IXmlLineInfo interface etc.) – George Birbilis Sep 25 '14 at 13:31
1

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

Benni
  • 203
  • 2
  • 7