XNamespace Class

Definition

代表 XML 命名空間。Represents an XML namespace. 這個類別無法被繼承。This class cannot be inherited.

public ref class XNamespace sealed
public sealed class XNamespace
type XNamespace = class
Public NotInheritable Class XNamespace
Inheritance
XNamespace

Remarks

此類別代表命名空間的 XML 結構。This class represents the XML construct of namespaces.

每個 XName 都包含 XNamespaceEvery XName contains an XNamespace. 即使項目不在命名空間中,項目的 XName 仍然會包含命名空間 XNamespace.NoneEven if an element is not in a namespace, the element's XName still contains a namespace, XNamespace.None. 不保證 XName.Namespace 屬性為 nullThe XName.Namespace property is guaranteed to not be null.

建立 XNamespace 物件Creating an XNamespace Object

建立 XNamespace 物件最常見的方式,就是直接指派字串給它。The most common way to create an XNamespace object is to simply assign a string to it. 接著,您可以使用加法運算子的覆寫,將命名空間與區功能變數名稱稱結合。You can then combine the namespace with a local name by using the override of the addition operator. 下列範例顯示此用法:The following example shows this idiom:

XNamespace aw = "http://www.adventure-works.com";  
XElement root = new XElement(aw + "Root", "Content");  
Console.WriteLine(root);  
Dim aw As XNamespace = "http://www.adventure-works.com"  
Dim root As XElement = New XElement(aw + "Root", "Content")  
Console.WriteLine(root)  

不過,在 Visual Basic 中,您通常會宣告全域預設命名空間,如下所示:However, in Visual Basic, you would typically declare a global default namespace, as follows:

Imports <xmlns='http://www.adventure-works.com'>  
  
Module Module1  
    Sub Main()  
        Dim root As XElement = _  
            <Root>Content</Root>  
        Console.WriteLine(root)  
    End Sub  
End Module  

這個範例會產生下列輸出:This example produces the following output:

<Root xmlns="http://www.adventure-works.com">Content</Root>  

將字串指派給 XNamespace 會使用來自 String的隱含轉換。Assigning a string to an XNamespace uses the implicit conversion from String.

如需詳細資訊和範例,請參閱如何C#:建立包含命名空間的檔()(LINQ to XML)See How to: Create a Document with Namespaces (C#) (LINQ to XML) for more information and examples.

如需在 Visual Basic 中使用命名空間的詳細資訊,請參閱Visual Basic (LINQ to XML)中的命名空間See Namespaces in Visual Basic (LINQ to XML) for more information on using namespaces in Visual Basic.

控制命名空間前置詞Controlling Namespace Prefixes

如果您建立宣告命名空間的屬性,則在屬性中指定的前置詞將會保存在序列化的 XML 中。If you create an attribute that declares a namespace, the prefix specified in the attribute will be persisted in the serialized XML. 若要建立宣告具有特定前置詞之命名空間的屬性,您可以建立屬性,其中之屬性名稱的命名空間為 Xmlns,而屬性的名稱為命名空間前置詞。To create an attribute that declares a namespace with a prefix, you create an attribute where the namespace of the name of the attribute is Xmlns, and the name of the attribute is the namespace prefix. 屬性的值為命名空間的 URI。The value of the attribute is the URI of the namespace. 下列範例顯示此用法:The following example shows this idiom:

XNamespace aw = "http://www.adventure-works.com";  
XElement root = new XElement(aw + "Root",  
    new XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"),  
    "Content");  
Console.WriteLine(root);  
Dim aw As XNamespace = "http://www.adventure-works.com"  
Dim root As XElement = New XElement(aw + "Root", _  
    New XAttribute(XNamespace.Xmlns + "aw", "http://www.adventure-works.com"), _  
    "Content")  
Console.WriteLine(root)  

在 Visual Basic 中,您通常會使用全域命名空間宣告,而不是建立命名空間節點來控制命名空間前置詞:In Visual Basic, instead of creating a namespace node to control namespace prefixes, you would typically use a global namespace declaration:

Imports <xmlns:aw='http://www.adventure-works.com'>  
  
Module Module1  
    Sub Main()  
        Dim root As XElement = _  
            <aw:Root>Content</aw:Root>  
        Console.WriteLine(root)  
    End Sub  
End Module  

這個範例會產生下列輸出:This example produces the following output:

<aw:Root xmlns:aw="http://www.adventure-works.com">Content</aw:Root>  

如需詳細資訊,請參閱如何:控制命名空間C#前置詞()(LINQ to XML)For more information, see How to: Control Namespace Prefixes (C#) (LINQ to XML).

建立預設命名空間Creating a Default Namespace

當您建立將成為命名空間的屬性時,如果屬性名稱具有 "xmlns" 的特殊值,則在序列化 XML 樹狀結構時,會將命名空間宣告為預設命名空間。When constructing an attribute that will be a namespace, if the attribute name has the special value of "xmlns", then when the XML tree is serialized, the namespace will be declared as the default namespace. 名稱為 "xmlns" 的特殊屬性不在任何命名空間中。The special attribute with the name of "xmlns" itself is not in any namespace. 屬性的值為命名空間 URI。The value of the attribute is the namespace URI.

下列範例會建立 XML 樹狀結構,其中包含的屬性是以命名空間將成為預設命名空間的方式宣告:The following example creates an XML tree that contains an attribute that is declared in such a way that the namespace will become the default namespace:

XNamespace aw = "http://www.adventure-works.com";  
XElement root = new XElement(aw + "Root",  
    new XAttribute("xmlns", "http://www.adventure-works.com"),  
    new XElement(aw + "Child", "content")  
);  
Console.WriteLine(root);  
Dim aw As XNamespace = "http://www.adventure-works.com"  
Dim root As XElement = New XElement(aw + "Root", _  
    New XAttribute("xmlns", "http://www.adventure-works.com"), _  
    New XElement(aw + "Child", "content") _  
)  
Console.WriteLine(root)  

在 Visual Basic 中,您通常會使用全域預設命名空間宣告,而不是建立命名空間節點來建立預設命名空間:In Visual Basic, instead of creating a namespace node to create a default namespace, you would typically use a global default namespace declaration:

Imports <xmlns='http://www.adventure-works.com'>  
  
Module Module1  
    Sub Main()  
        Dim root As XElement = _  
            <Root>  
                <Child>content</Child>  
            </Root>  
        Console.WriteLine(root)  
    End Sub  
End Module  

這個範例會產生下列輸出:This example produces the following output:

<Root xmlns="http://www.adventure-works.com">  
  <Child>content</Child>  
</Root>  

XNamespace 不可部分完成XNamespace Atomization

XNamespace 物件保證是不可部分完成的;也就是說,如果兩個 XNamespace 物件有完全相同的 URI,則會共用相同的實例。XNamespace objects are guaranteed to be atomized; that is, if two XNamespace objects have exactly the same URI, they will share the same instance. 針對此用途,會明確提供相等與比較運算子。The equality and comparison operators are provided explicitly for this purpose.

使用擴充的名稱Using Expanded Names

指定命名空間和本機名稱的另一種方式,是使用 {namespace}name格式的擴充名稱:Another way to specify a namespace and a local name is to use an expanded name in the form {namespace}name:

XElement e = new XElement("{http://www.adventure-works.com}Root",  
     new XAttribute("{http://www.adventure-works.com}Att", "content")  
);  
Console.WriteLine(e);  
Dim e As XElement = New XElement("{http://www.adventure-works.com}Root", _  
     New XAttribute("{http://www.adventure-works.com}Att", "content") _  
)  
Console.WriteLine(e)  

這個範例會產生下列輸出:This example produces the following output:

<Root p1:Att="content" xmlns:p1="http://www.adventure-works.com" xmlns="http://www.adventure-works.com" />  

這個方法會有效能隱含作用。This approach has performance implications. 每次您將包含擴充名稱的字串傳遞到 LINQ to XMLLINQ to XML 時,此方法必須剖析名稱、尋找不可部分完成的命名空間,然後尋找不可部分完成的名稱。Each time that you pass a string that contains an expanded name to LINQ to XMLLINQ to XML, it must parse the name, find the atomized namespace, and find the atomized name. 這個程序會使用 CPU 時間。This process takes CPU time. 如果效能很重要,您可能會想要使用不同的方法。If performance is important, you may want to use a different approach.

使用 Visual Basic,建議的方法是使用 XML 常值,而不需要使用擴充的名稱。With Visual Basic, the recommended approach is to use XML literals, which does not involve the use of expanded names.

Properties

NamespaceName

取得這個命名空間的統一資源識別元 (URI)。Gets the Uniform Resource Identifier (URI) of this namespace.

None

取得與無命名空間相對應的 XNamespace 物件。Gets the XNamespace object that corresponds to no namespace.

Xml

取得與 XML URI (http://www.w3.org/XML/1998/namespace) 相對應的 XNamespace 物件。Gets the XNamespace object that corresponds to the XML URI (http://www.w3.org/XML/1998/namespace).

Xmlns

取得與 xmlns URI (http://www.w3.org/2000/xmlns/) (英文) 相對應的 XNamespace 物件。Gets the XNamespace object that corresponds to the xmlns URI (http://www.w3.org/2000/xmlns/).

Methods

Equals(Object)

決定特定的 XNamespace 是否與目前的 XNamespace 相等。Determines whether the specified XNamespace is equal to the current XNamespace.

Get(String)

取得指定之統一資源識別元 (URI) 的 XNamespaceGets an XNamespace for the specified Uniform Resource Identifier (URI).

GetHashCode()

取得這個 XNamespace 的雜湊碼。Gets a hash code for this XNamespace.

GetName(String)

傳回從這個 XName 建立的 XNamespace 物件,以及指定的區域名稱。Returns an XName object created from this XNamespace and the specified local name.

GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

傳回這個 XNamespace 的 URI。Returns the URI of this XNamespace.

Operators

Addition(XNamespace, String)

XNamespace 物件與區域名稱結合在一起以建立 XNameCombines an XNamespace object with a local name to create an XName.

Equality(XNamespace, XNamespace)

傳回數值,指出 XNamespace 的兩個執行個體是否相等。Returns a value indicating whether two instances of XNamespace are equal.

Implicit(String to XNamespace)

將包含統一資源識別元 (URI) 的字串轉換為 XNamespaceConverts a string containing a Uniform Resource Identifier (URI) to an XNamespace.

Inequality(XNamespace, XNamespace)

傳回值,這個值指出 XNamespace 的兩個執行個體是否不相等。Returns a value indicating whether two instances of XNamespace are not equal.

Applies to

See also