如何在 C# 中创建包含命名空间的文档(LINQ to XML)
本文介绍如何在具有命名空间的 C# 中创建文档。
示例:声明和初始化默认命名空间
若要创建命名空间中的元素或属性,首先声明并初始化 XNamespace 对象。 然后,使用加法运算符重载将命名空间与本地名称合并,以字符串表示。
以下示例创建一个包含一个命名空间的文档。 默认情况下,LINQ to XML 使用默认命名空间序列化此文档。
// Create an XML tree in a namespace.
XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "Root",
new XElement(aw + "Child", "child content")
);
Console.WriteLine(root);
此示例生成以下输出:
<Root xmlns="http://www.adventure-works.com">
<Child>child content</Child>
</Root>
示例:创建具有命名空间和属性的文档
以下示例创建一个包含一个命名空间的文档。 它还创建了一个属性,该属性使用命名空间前缀来声明命名空间。 若要创建声明具有前缀的命名空间的属性,请创建一个属性,其中该属性的名称为命名空间前缀,并且此名称位于 Xmlns 命名空间中。 此属性的值是命名空间的 URI。
// Create an XML tree in a namespace, with a specified prefix
XNamespace aw = "http://www.adventure-works.com";
XElement root = new XElement(aw + "Root",
new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),
new XElement(aw + "Child", "child content")
);
Console.WriteLine(root);
此示例生成以下输出:
<aw:Root xmlns:aw="http://www.adventure-works.com">
<aw:Child>child content</aw:Child>
</aw:Root>
示例:创建包含两个命名空间的文档,一个具有前缀
下面的示例演示如何创建包含两个命名空间的文档。 一个是默认命名空间,另一个是具有前缀的命名空间。
通过在根元素中包含命名空间属性,命名空间会被序列化,其中 http://www.adventure-works.com
为默认命名空间,而 www.fourthcoffee.com
会带有前缀 fc
进行序列化。 若要创建声明默认命名空间的属性,请使用名称 xmlns
创建一个属性,而不创建命名空间。 属性的值是默认命名空间 URI。
如果默认命名空间声明在范围内,则它通过为其本地名称加上相应的 XNamespace
对象的前缀来应用于子 XElement
对象。 另一方面,默认命名空间声明不直接应用于属性名称。 因此,默认命名空间中的 XAttribute
对象是通过 而非,在其本地名称前添加对应的 XNamespace
对象的前缀来定义的。
// The http://www.adventure-works.com namespace is forced to be the default namespace.
XNamespace aw = "http://www.adventure-works.com";
XNamespace fc = "www.fourthcoffee.com";
XElement root = new XElement(aw + "Root",
new XAttribute("xmlns", "http://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 XAttribute("DefaultNs", "default namespace"),
new XAttribute(fc + "PrefixedNs", "prefixed namespace")
),
new XElement(fc + "Child3", "c3 content",
new XAttribute("DefaultNs", "default namespace"),
new XAttribute(fc + "PrefixedNs", "prefixed namespace")
)
);
Console.WriteLine(root);
此示例生成以下输出:
<Root xmlns="http://www.adventure-works.com" xmlns:fc="www.fourthcoffee.com">
<fc:Child>
<DifferentChild>other content</DifferentChild>
</fc:Child>
<Child2 DefaultNs="default namespace" fc:PrefixedNs="prefixed namespace">c2 content</Child2>
<fc:Child3 DefaultNs="default namespace" fc:PrefixedNs="prefixed namespace">c3 content</fc:Child3>
</Root>
示例:创建包含两个命名空间的文档,两个命名空间都带有前缀
以下示例创建一个文档,其中包含两个命名空间,两个命名空间都带有命名空间前缀。
XNamespace aw = "http://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="http://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 对象的预原子化 了解详细信息。
// Create an XML tree in a namespace, with a specified prefix
XElement root = new XElement("{http://www.adventure-works.com}Root",
new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),
new XElement("{http://www.adventure-works.com}Child", "child content")
);
Console.WriteLine(root);
此示例生成以下输出:
<aw:Root xmlns:aw="http://www.adventure-works.com">
<aw:Child>child content</aw:Child>
</aw:Root>