HOW TO:利用命名空間建立文件 (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>
下列範例顯示如何建立包含兩個命名空間的文件。 一個是預設命名空間。 另一個是具有前置詞的命名空間。
藉由將命名空間屬性包含到根項目 (Root Element) 中,系統會序列化命名空間,讓 https://www.adventure-works.com 成為預設命名空間,而 www.fourthcoffee.com 則利用 "fc" 的前置詞序列化。 若要建立宣告預設命名空間的屬性,您可以建立名稱為 "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>