如何:创建包含命名空间的文档 (C#) (LINQ to XML)
本主题演示如何创建包含命名空间的文档。
示例
若要创建一个属于命名空间的元素或属性,请首先声明和初始化一个 XNamespace 对象。 然后使用加法运算符重载来组合命名空间和本地名称(以字符串表示)。
下面的示例创建一个包含一个命名空间的文档。 默认情况下,LINQ to XML 使用默认命名空间序列化此文档。
// Create an XML tree in a namespace.
XNamespace aw = "https://www.adventure-works.com";
XElement root = new XElement(aw + "Root",
new XElement(aw + "Child", "child content")
);
Console.WriteLine(root);
此示例产生以下输出:
<Root xmlns="https://www.adventure-works.com">
<Child>child content</Child>
</Root>
下面的示例创建一个包含一个命名空间的文档。 另外,还创建一个属性,该属性声明具有命名空间前缀的命名空间。 若要创建一个声明具有前缀的命名空间的属性,请创建一个属性,其中该属性的名称为命名空间前缀,并且此名称位于 Xmlns 命名空间中。 此属性的值即是命名空间的 URI。
// Create an XML tree in a namespace, with a specified prefix
XNamespace aw = "https://www.adventure-works.com";
XElement root = new XElement(aw + "Root",
new XAttribute(XNamespace.Xmlns + "aw", "https://www.adventure-works.com"),
new XElement(aw + "Child", "child content")
);
Console.WriteLine(root);
此示例产生以下输出:
<aw:Root xmlns:aw="https://www.adventure-works.com">
<aw:Child>child content</aw:Child>
</aw:Root>
下面的示例演示如何创建一个包含两个命名空间的文档。 一个是默认命名空间。 另一个是具有前缀的命名空间。
通过在根元素中包括命名空间属性,对命名空间进行了序列化,以便 https://www.adventure-works.com 是默认命名空间,而使用“fc”前缀对 www.fourthcoffee.com 进行序列化。 若要创建一个声明默认命名空间的属性,请创建一个名称为“xmlns”的属性,而无需命名空间。 该属性的值即是默认命名空间 URI。
// The https://www.adventure-works.com namespace is forced to be the default namespace.
XNamespace aw = "https://www.adventure-works.com";
XNamespace fc = "www.fourthcoffee.com";
XElement root = new XElement(aw + "Root",
new XAttribute("xmlns", "https://www.adventure-works.com"),
new XAttribute(XNamespace.Xmlns + "fc", "www.fourthcoffee.com"),
new XElement(fc + "Child",
new XElement(aw + "DifferentChild", "other content")
),
new XElement(aw + "Child2", "c2 content"),
new XElement(fc + "Child3", "c3 content")
);
Console.WriteLine(root);
此示例产生以下输出:
<Root xmlns="https://www.adventure-works.com" xmlns:fc="www.fourthcoffee.com">
<fc:Child>
<DifferentChild>other content</DifferentChild>
</fc:Child>
<Child2>c2 content</Child2>
<fc:Child3>c3 content</fc:Child3>
</Root>
下面的示例创建一个包含两个命名空间的文档,这两个命名空间都具有命名空间前缀。
XNamespace aw = "https://www.adventure-works.com";
XNamespace fc = "www.fourthcoffee.com";
XElement root = new XElement(aw + "Root",
new XAttribute(XNamespace.Xmlns + "aw", aw.NamespaceName),
new XAttribute(XNamespace.Xmlns + "fc", fc.NamespaceName),
new XElement(fc + "Child",
new XElement(aw + "DifferentChild", "other content")
),
new XElement(aw + "Child2", "c2 content"),
new XElement(fc + "Child3", "c3 content")
);
Console.WriteLine(root);
此示例产生以下输出:
<aw:Root xmlns:aw="https://www.adventure-works.com" xmlns:fc="www.fourthcoffee.com">
<fc:Child>
<aw:DifferentChild>other content</aw:DifferentChild>
</fc:Child>
<aw:Child2>c2 content</aw:Child2>
<fc:Child3>c3 content</fc:Child3>
</aw:Root>
另一种可获得相同结果的方法是使用扩展名,而不是声明和创建一个 XNamespace 对象。
这种方法的性能较低。 每次将包含扩展名的字符串传递给 LINQ to XML 时,LINQ to XML 都必须分析名称,查找原子化命名空间,再查找原子化名称。 这个过程会占用 CPU 时间。 如果性能很重要,则您可能希望显式声明和使用 XNamespace 对象。
如果性能是一个重要问题,请参见XName 对象的预原子化 (LINQ to XML) 获取更多信息
// Create an XML tree in a namespace, with a specified prefix
XElement root = new XElement("{https://www.adventure-works.com}Root",
new XAttribute(XNamespace.Xmlns + "aw", "https://www.adventure-works.com"),
new XElement("{https://www.adventure-works.com}Child", "child content")
);
Console.WriteLine(root);
此示例产生以下输出:
<aw:Root xmlns:aw="https://www.adventure-works.com">
<aw:Child>child content</aw:Child>
</aw:Root>