如何在 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 是預設命名空間,然後使用 fc 前置詞序列化 www.fourthcoffee.com。 若要建立宣告預設命名空間的屬性,請建立名為 xmlns 的屬性,但不包含命名空間。 屬性的值為預設的命名空間 URI。

// 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 XElement(fc + "Child3", "c3 content")
);
Console.WriteLine(root);

這個範例會產生下列輸出:

<Root xmlns="http://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 = "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>

另請參閱