Upravit

Sdílet prostřednictvím


XName.Get Method

Definition

Gets an XName object.

Overloads

Get(String)

Gets an XName object from an expanded name.

Get(String, String)

Gets an XName object from a local name and a namespace.

Remarks

This method provides overloads that allow you to create an XName from a expanded XML name. You can create an XName from a string in the form {namespace}localname, or from a namespace and a local name, specified separately.

A much more common and easier way to create an XName is to use the implicit conversion from string. 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.

For more information and examples, see How to create a document with namespaces in C#.

For more information on using namespaces, see Work with XML namespace.

Because XName objects are atomized, if there is an existing XName with exactly the same name, the assigned variable will refer to the existing XName. If there is no existing XName, a new one will be created and initialized.

Get(String)

Source:
XName.cs
Source:
XName.cs
Source:
XName.cs

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

Parameters

expandedName
String

A String that contains an expanded XML name in the format {namespace}localname.

Returns

An XName object constructed from the expanded name.

Examples

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>

Remarks

This method contains overloads that allow you to create an XName. You can create it from a expanded XML name in the form {namespace}localname, or from a namespace and a local name, specified separately.

A much more common and easier way to create an XName is to use the implicit conversion from string.

Because XName are atomized, if there is an existing XName with exactly the same name, the assigned variable will refer to the existing XName. If there is no existing XName, a new one will be created and initialized.

See also

Applies to

Get(String, String)

Source:
XName.cs
Source:
XName.cs
Source:
XName.cs

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

Parameters

localName
String

A local (unqualified) name.

namespaceName
String

An XML namespace.

Returns

An XName object created from the specified local name and namespace.

Examples

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>

Remarks

This method contains overloads that allow you to create an XName. You can create it from an expanded XML name in the form {namespace}localname, or from a namespace and a local name, specified separately.

A much more common and easier way to create an XName is to use the implicit conversion from string.

Because XName are atomized, if there is an existing XName with exactly the same name, the assigned variable will refer to the existing XName. If there is no existing XName, a new one will be created and initialized.

See also

Applies to