C# 如何将外部XML包含到XML文件中-这两个文件应该是什么样子

C# 如何将外部XML包含到XML文件中-这两个文件应该是什么样子,c#,.net,xml,linq,linq-to-xml,C#,.net,Xml,Linq,Linq To Xml,我正在使用.NET C#XML和LINQ更新/删除/插入我的XML文件。 我有一个如下所示的XML文件(image1),我只想包含第二个XML文件 第一个XML文件是原始文件,我不想碰这个文件。所以我更喜欢第二个。文件(外部文件),因此我可以在那里添加/删除XML行,而不是mainfXML文件 但是如何将第二个XML(外部文件)包含或合并到第一个XML中呢? 我只需要从红色框中查看标签(见红色框) <RewriterRule> <LookFor> &l

我正在使用.NET C#XML和LINQ更新/删除/插入我的XML文件。

我有一个如下所示的XML文件(image1),我只想包含第二个XML文件

第一个XML文件是原始文件,我不想碰这个文件。所以我更喜欢第二个。文件(外部文件),因此我可以在那里添加/删除XML行,而不是mainfXML文件

但是如何将第二个XML(外部文件)包含或合并到第一个XML中呢? 我只需要从红色框中查看标签(见红色框)

<RewriterRule>
   <LookFor>        </LookFor>
   <SendTo>         </SendTo>
</RewriterRule>

问题:

1-为了包含XML文件2(外部文件)中的代码,我需要为XML文件1中的代码编写什么

2-我的XMLfile 2(外部文件)应该是什么样子?事实上,我需要我认为的标记,因为我正在读取XML
XDocument xdoc=XDocument.Load(this.Server.MapPath(path))并执行一些更新/删除

IMAG2-外部文件
从第二个文件添加元素很容易:

XDocument doc1 = XDocument.Load(MapPath("file1.xml"));
doc1.Root.Element("Rules").Add(XDocument.Load(MapPath("file2.xml")).Root.Element("Rules").Elements("RewriterRule"));

// now save to third file with e.g.
doc1.Save(MapPath("updated.xml"));
// or overwrite first file e.g.
doc1.Save(MapPath("file1.xml"));
另一方面,术语“合并”表示您可能希望执行更复杂的操作,例如基于某个id或键识别元素,然后不只是添加新元素,而是覆盖某些数据。如果您在编写代码时需要帮助,则需要提供更多详细信息,说明您希望合并过程的具体类型

[编辑]下面是一个示例,介绍如何使用基于DTD的外部实体引用机制将XML片段文件包含到另一个文档中:file1.XML如下所示:

<!DOCTYPE example [
  <!ENTITY e1 SYSTEM "fragment.xml">
]>
<example>
  <data>
    <item>1</item>
    &e1;
  </data>
</example>
<?xml version="1.0" encoding="utf-8" ?>
<item>2</item>

从第二个文件添加元素很容易:

XDocument doc1 = XDocument.Load(MapPath("file1.xml"));
doc1.Root.Element("Rules").Add(XDocument.Load(MapPath("file2.xml")).Root.Element("Rules").Elements("RewriterRule"));

// now save to third file with e.g.
doc1.Save(MapPath("updated.xml"));
// or overwrite first file e.g.
doc1.Save(MapPath("file1.xml"));
另一方面,术语“合并”表示您可能希望执行更复杂的操作,例如基于某个id或键识别元素,然后不只是添加新元素,而是覆盖某些数据。如果您在编写代码时需要帮助,则需要提供更多详细信息,说明您希望合并过程的具体类型

[编辑]下面是一个示例,介绍如何使用基于DTD的外部实体引用机制将XML片段文件包含到另一个文档中:file1.XML如下所示:

<!DOCTYPE example [
  <!ENTITY e1 SYSTEM "fragment.xml">
]>
<example>
  <data>
    <item>1</item>
    &e1;
  </data>
</example>
<?xml version="1.0" encoding="utf-8" ?>
<item>2</item>

使用XInclude W3C标记有一种更好的方法。以下是我的Linq到XML扩展方法:

/// <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
}
//
///Linq到XML XInclude扩展
/// 
公共静态类XincludeExtension
{
#区域字段
/// 
///W3C XInclude标准
///注意2001年和2003年的不同标准。
/// 
公共静态只读XNamespace IncludeNamespace=”http://www.w3.org/2003/XInclude";
/// 
///包含元素名
/// 
公共静态只读XName IncludeElementName=IncludeNamespace+“include”;
/// 
///包含位置属性
/// 
公用常量字符串IncludeLocationAttributeName=“href”;
/// 
///定义最大子包含计数25
/// 
public const int MaxSubIncludeCountDefault=25;
#端区
#区域方法
/// 
///将XInclude引用替换为目标内容。
///W3C标准:http://www.w3.org/2003/XInclude
/// 
///xml文档。
///允许的最大嵌套xml包括(默认值:25)。
公共静态void ReplaceXIncludes(此XDocument xDoc,int maxSubIncludeCount=MaxSubIncludeCountDefault)
{
ReplaceXIncludes(xDoc.Root,maxSubIncludeCount);
}
/// 
///将XInclude引用替换为目标内容。
///W3C标准:http://www.w3.org/2003/XInclude
/// 
///XML元素。
///允许的最大嵌套xml包括(默认值:25)。
公共静态void ReplaceXIncludes(此XElement xmlElement,int-maxSubIncludeCount=MaxSubIncludeCountDefault)
{
ReplaceXIncludes(1,maxSubIncludeCount);
}
私有静态void ReplaceXIncludes(此XElement xmlElement、int-subIncludeCount、int-maxSubIncludeCount)
{
var results=xmlElement.DegenantsAndSelf(IncludeElementName).ToArray();//必须具体化
foreach(结果中的var includeElement)
{
var path=includeElement.Attribute(IncludeLocationAttributeName).Value;
path=path.GetFullPath(path);
var doc=XDocument.Load(路径);

如果(subIncludeCount使用XInclude W3C标记有更好的方法。下面是我的Linq到XML扩展方法:

/// <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
}
//
///Linq到XML XInclude扩展
/// 
公共静态类XincludeExtension
{
#区域字段
/// 
///W3C XInclude标准
///注意2001年和2003年的不同标准。
/// 
公共静态只读XNamespace IncludeNamespace=”http://www.w3.org/2003/XInclude";
/// 
///包含元素名
/// 
公共静态只读XName IncludeElementName=IncludeNamespace+“include”;
/// 
///包含位置属性
/// 
公用常量字符串IncludeLocationAttributeName=“href”;
/// 
///定义最大子包含计数25
/// 
public const int MaxSubIncludeCountDefault=25;
#端区
#区域方法
/// 
///将XInclude引用替换为目标内容。
///W3C标准:http://www.w3.org/2003/XInclude
/// 
///xml文档。
///允许的最大嵌套xml包括(默认值:25)。
公共静态void ReplaceXIncludes(此XDocument xDoc,int maxSubIncludeCount=MaxSubIncludeCountDefault)
{
ReplaceXIncludes(xDoc.Root,maxSubIncludeCount);
}
/// 
///将XInclude引用替换为目标内容。
///W3C标准:http://www.w3.org/2003/XInclude
/// 
///XML元素。
///允许的最大嵌套xml包括(默认值:25)。
公共静态void ReplaceXIncludes(此XElement xmlElement,int-maxSubIncludeCount=MaxSubIncludeCountDefault)
{
ReplaceXIncludes(1,maxSubIncludeCount);
}
私有静态无效重放