如何在 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。
// 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必須剖析名稱、尋找 Atomized 命名空間,以及尋找 atomized 名稱。 這個程序會使用 CPU 時間。 如果效能對您很重要,您可能會想要明確宣告並使用 XNamespace 物件。
如果效能是重要的問題,請參閱 XName 物件的預先 Atom 化 以取得詳細資訊。
// 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>