据我所知,.net不支持XInclude。
我想利用同样的机制来处理分层组织的XML配置文件。我的意思是有一个顶级XML配置文件引用特定的XML文件。我的配置是一组专门用于一个特定模块的配置。
应该如何处理?(或者为什么不应该这样做...)
我想利用同样的机制来处理分层组织的XML配置文件。我的意思是有一个顶级XML配置文件引用特定的XML文件。我的配置是一组专门用于一个特定模块的配置。
应该如何处理?(或者为什么不应该这样做...)
我不使用.net,但你可以尝试使用实体...
<!DOCTYPE example [
<!ENTITY doc1 SYSTEM "doc1.xml">
<!ENTITY doc2 SYSTEM "doc2.xml">
]>
<example>
&doc1;
&doc2;
</example>
/// <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://www.microsoft.com/en-us/download/details.aspx?id=4972
应该会有所帮助。
还有第三方解决方案。
您可以扩展XmlReader以读取XInclude。