如何使用 'include' 语句创建 Xml 文档

How to create Xml document with 'include' statement

我以前设法编写代码来制作简单的 XML 文档,但我需要编写一个具有 XML 'include' 属性的 XML 文档。

这是我需要复制的示例:

<?xml version="1.0" encoding="utf-8"?>
<document xmlns:xi="http://www.w3.org/2001/XInclude">
   <xi:include href="./file.xml"/>
   <data>
   </data>
</document>

网络上有很多关于这个主题的信息,但它似乎在很大程度上超出了我的理解范围,而且主要涉及 'include' 语句的阅读和处理。我只需要将其写入一个 XML,每次都完全相同。

这是我尝试过的方法,但它显然不应该以这种方式工作,它不会让我使用空白标签。

public static void WriteXml()
    {           
        // Create the xml document in memory
        XmlDocument myXml = new XmlDocument();

        // Append deceleration
        XmlDeclaration xmlDec = myXml.CreateXmlDeclaration("1.0", "UTF-8", null);
        myXml.AppendChild(xmlDec);

        // Append root node
        XmlElement rootNode = myXml.CreateElement("document");
        rootNode.SetAttribute("xmlns:xi", "http://www.w3.org/2001/XInclude");            
        myXml.AppendChild(rootNode);

        //  Append includes statement
        XmlElement xiStatement = myXml.CreateElement("");
        xiStatement.SetAttribute("xi:include", "./file.xml");
        rootNode.AppendChild(xiStatement);

        myXml.Save(@"C:\Temp\testxml.xml");
    }

任何人都可以建议一种添加 includes 语句的简单方法吗?

编辑:

如果我使用下面的,它更接近我的目标,但添加 href 属性似乎导致标签 "xi:include" 自动更改为 "include"。

public static void WriteXml()
    {
        // Create the xml document in memory
        XmlDocument myXml = new XmlDocument();

        // Append deceleration
        XmlDeclaration xmlDec = myXml.CreateXmlDeclaration("1.0", "UTF-8", null);
        myXml.AppendChild(xmlDec);

        // Append root node
        XmlElement rootNode = myXml.CreateElement("document");
        rootNode.SetAttribute("xmlns:xi", "http://www.w3.org/2001/XInclude");
        myXml.AppendChild(rootNode);

        //  Append includes statement
        XmlElement xiStatement = myXml.CreateElement("xi:include");
        xiStatement.SetAttribute("href", "./file.xml");
        rootNode.AppendChild(xiStatement);

        myXml.Save(@"C:\Temp\testxml.xml");
    }

首先,如前所述,您的 include 是一个 元素 href 是一个 属性 ,因此您可以按如下方式创建它:

var doc = new XmlDocument();

var declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(declaration);

var root = doc.CreateElement("document");
root.SetAttribute("xmlns:xi", "http://www.w3.org/2001/XInclude");
doc.AppendChild(root);

var include = doc.CreateElement("xi", "include", "http://www.w3.org/2001/XInclude");
include.SetAttribute("href", "./file.xml");
root.AppendChild(include);

var data = doc.CreateElement("data");
root.AppendChild(data);

但更简单的解决方案是使用 LINQ 来 XML,这是一种更具表现力的 API。您可以通过多种方式使用它,一种方式是:

XNamespace include = "http://www.w3.org/2001/XInclude";

var doc = new XDocument(
    new XDeclaration("1.0", "UTF-8", null),
    new XElement("document",
        new XAttribute(XNamespace.Xmlns + "xi", include),
        new XElement(include + "include",
            new XAttribute("href", "./file.xml")
            ),
        new XElement("data")
        )            
    );