【问题标题】:How to create Xml document with 'include' statement如何使用“包含”语句创建 Xml 文档
【发布时间】:2015-10-23 00:00:56
【问题描述】:

我之前已经设法编写代码来制作简单的 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>

网上有很多关于这个主题的信息,但它似乎大部分都在我之上,主要是关于“包含”语句的阅读和处理。我只需要将其写入 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");
    }

谁能建议一种简单的方法来添加包含语句?

编辑:

如果我使用以下,它更接近我的目标,但添加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");
    }

【问题讨论】:

  • 在上面的例子中,它会抛出一个参数异常:元素或属性的本地名称不能为空或空字符串。
  • 我尝试将属性文本移动到元素标记,它不会引发异常,但 xml 说: 而不是
  • 如果添加“./file.xml”作为href属性,它会将标签中的“xi:include”更改为“include”
  • 已添加代码。我不知道如何使用Linq。几个月前刚开始编码,但我的老板让我一头扎进了一些小项目(我很喜欢)。

标签: c# xml include


【解决方案1】:

首先,如前所述,您的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 to 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")
        )            
    );

【讨论】:

  • 谢谢查尔斯。这完美无缺。现在我将我的示例视为 LINQ,我可以看到它是如何构建它的更好方法。谢谢。
猜你喜欢
  • 2020-05-15
  • 2012-07-14
  • 2015-10-25
  • 2021-06-22
  • 2022-01-20
  • 2011-02-25
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多