如何在 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
物件名稱之前,從而套用至這些子系 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>