XName.Get 方法

定义

获取一个 XName 对象。Gets an XName object.

重载

Get(String)

从展开名称获取 XName 对象。Gets an XName object from an expanded name.

Get(String, String)

从本地名称和命名空间获取 XName 对象。Gets an XName object from a local name and a namespace.

注解

此方法提供重载,使你能够 XName 从扩展的 XML 名称创建。This method provides overloads that allow you to create an XName from a expanded XML name. 你可以 XName 从窗体中的字符串创建 {namespace}localname ,或通过单独指定的命名空间和本地名称来创建。You can create an XName from a string in the form {namespace}localname, or from a namespace and a local name, specified separately.

创建的更常见且更简单的方法 XName 是使用字符串的隐式转换。A much more common and easier way to create an XName is to use the implicit conversion from string. 若要创建命名空间中的名称,常用方法是使用加法运算符重载,以允许你组合 XNamespace 对象和字符串。To create a name that is in a namespace, the common approach is to use the addition operator overload that allows you to combine an XNamespace object and a string.

有关详细信息和示例,请参阅 如何使用 c # 创建包含命名空间的文档For more information and examples, see How to create a document with namespaces in C#.

有关使用命名空间的详细信息,请参阅 使用 XML 命名空间For more information on using namespaces, see Work with XML namespace.

由于 XName 对象是原子化的,因此,如果现有 XName 的具有完全相同的名称,则分配的变量将引用现有的 XNameBecause XName objects are atomized, if there is an existing XName with exactly the same name, the assigned variable will refer to the existing XName. 如果没有现有 XName 的,则将创建并初始化一个新的。If there is no existing XName, a new one will be created and initialized.

Get(String)

从展开名称获取 XName 对象。Gets an XName object from an expanded name.

public:
 static System::Xml::Linq::XName ^ Get(System::String ^ expandedName);
public static System.Xml.Linq.XName Get (string expandedName);
static member Get : string -> System.Xml.Linq.XName
Public Shared Function Get (expandedName As String) As XName

参数

expandedName
String

一个 String,其中包含格式为 {namespace}localname 的展开 XML 名称。A String that contains an expanded XML name in the format {namespace}localname.

返回

XName

从展开名称构造的 XName 对象。An XName object constructed from the expanded name.

示例

下面的示例演示如何使用此方法。The following example shows the use of this method.

XName name = XName.Get("{http://www.adventure-works.com}Root");  
XElement el = new XElement(name, "content");  
Console.WriteLine(el);  

// This is the preferred approach for specifying the XName in the  
// constructor of XElement.  
XNamespace aw = "http://www.adventure-works.com";  
XElement el2 = new XElement(aw + "Root", "content");  
Console.WriteLine(el2);  
Imports <xmlns="http://www.adventure-works.com">  

Module Module1  
    Sub Main()  
        Dim name As XName = XName.Get("{http://www.adventure-works.com}Root")  
        Dim el As XElement = New XElement(name, "content")  
        Console.WriteLine(el)  

        ' The preferred approach for specifying an XName in a namespace  
        ' for Visual Basic is to import a global namespace.  
        Dim el2 As XElement = <Root>content</Root>  
        Console.WriteLine(el2)  
    End Sub  
End Module  

该示例产生下面的输出:This example produces the following output:

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

注解

此方法包含允许您创建的重载 XNameThis method contains overloads that allow you to create an XName. 您可以使用窗体中的扩展 XML 名称 {namespace}localname 或单独指定的命名空间和本地名称来创建它。You can create it from a expanded XML name in the form {namespace}localname, or from a namespace and a local name, specified separately.

创建的更常见且更简单的方法 XName 是使用字符串的隐式转换。A much more common and easier way to create an XName is to use the implicit conversion from string.

由于 XName 是原子化的,因此,如果现有 XName 的具有完全相同的名称,则分配的变量将引用现有的 XNameBecause XName are atomized, if there is an existing XName with exactly the same name, the assigned variable will refer to the existing XName. 如果没有现有 XName 的,则将创建并初始化一个新的。If there is no existing XName, a new one will be created and initialized.

适用于

Get(String, String)

从本地名称和命名空间获取 XName 对象。Gets an XName object from a local name and a namespace.

public:
 static System::Xml::Linq::XName ^ Get(System::String ^ localName, System::String ^ namespaceName);
public static System.Xml.Linq.XName Get (string localName, string namespaceName);
static member Get : string * string -> System.Xml.Linq.XName
Public Shared Function Get (localName As String, namespaceName As String) As XName

参数

localName
String

本地(非限定)名称。A local (unqualified) name.

namespaceName
String

一个 XML 命名空间。An XML namespace.

返回

XName

使用指定的本地名称和命名空间创建的 XName 对象。An XName object created from the specified local name and namespace.

示例

下面的示例演示如何使用此方法。The following example shows the use of this method.

XName name = XName.Get("Root", "http://www.adventure-works.com");  
XElement el = new XElement(name, "content");  
Console.WriteLine(el);  

// This is the preferred form.  
XNamespace aw = "http://www.adventure-works.com";  
XElement el2 = new XElement(aw + "Root", "content");  
Console.WriteLine(el2);  
Imports <xmlns="http://www.adventure-works.com">  

Module Module1  
    Sub Main()  
        Dim name As XName = XName.Get("{http://www.adventure-works.com}Root")  
        Dim el As XElement = New XElement(name, "content")  
        Console.WriteLine(el)  

        ' The preferred approach for specifying an XName in a namespace  
        ' for Visual Basic is to import a global namespace.  
        Dim el2 As XElement = <Root>content</Root>  
        Console.WriteLine(el2)  
    End Sub  
End Module  

该示例产生下面的输出:This example produces the following output:

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

注解

此方法包含允许您创建的重载 XNameThis method contains overloads that allow you to create an XName. 您可以使用窗体中的扩展 XML 名称 {namespace}localname 或单独指定的命名空间和本地名称来创建它。You can create it from an expanded XML name in the form {namespace}localname, or from a namespace and a local name, specified separately.

创建的更常见且更简单的方法 XName 是使用字符串的隐式转换。A much more common and easier way to create an XName is to use the implicit conversion from string.

由于 XName 是原子化的,因此,如果现有 XName 的具有完全相同的名称,则分配的变量将引用现有的 XNameBecause XName are atomized, if there is an existing XName with exactly the same name, the assigned variable will refer to the existing XName. 如果没有现有 XName 的,则将创建并初始化一个新的。If there is no existing XName, a new one will be created and initialized.

适用于