XML Element Literal
A literal that represents an XElement object.
<name [ attributeList ] /> -or- <name [ attributeList ] > [ elementContents ] </[ name ]>
An XElement object.
You can use the XML element literal syntax to create XElement objects in your code.
Note: |
|---|
An XML literal can span multiple lines without using line continuation characters. This feature enables you to copy content from an XML document and paste it directly into a Visual Basic program. |
Embedded expressions of the form <%= exp %> enable you to add dynamic information to an XML element literal. For more information, see Embedded Expressions in XML.
The Visual Basic compiler converts the XML element literal into calls to the XElement constructor and, if it is required, the XAttribute constructor.
XML Namespaces
XML namespace prefixes are useful when you have to create XML literals with elements from the same namespace many times in code. You can use global XML namespace prefixes, which you define by using the Imports statement, or local prefixes, which you define by using the xmlns:xmlPrefix="xmlNamespace" attribute syntax. For more information, see Imports Statement (.NET Namespace and Type).
In accordance with the scoping rules for XML namespaces, local prefixes take precedence over global prefixes. However, if an XML literal defines an XML namespace, that namespace is not available to expressions that appear in an embedded expression. The embedded expression can access only the global XML namespace.
The Visual Basic compiler converts each global XML namespace that is used by an XML literal into a one local namespace definition in the generated code. Global XML namespaces that are not used do not appear in the generated code.
The following example shows how to create a simple XML element that has two nested empty elements.
Dim test1 As XElement = _ <outer> <inner1></inner1> <inner2/> </outer> Console.WriteLine(test1)
The example displays the following text. Notice that the literal preserves the structure of the empty elements.
<outer> <inner1></inner1> <inner2 /> </outer>
The following example shows how to use embedded expressions to name an element and create attributes.
Dim elementType As String = "book" Dim attributeName1 As String = "year" Dim attributeValue1 As Integer = 1999 Dim attributeName2 As String = "title" Dim attributeValue2 As String = "My Book" Dim book As XElement = _ <<%= elementType %> isbn="1234" <%= attributeName1 %>=<%= attributeValue1 %> <%= New XAttribute(attributeName2, attributeValue2) %> /> Console.WriteLine(book)
This code displays the following text:
<book isbn="1234" year="1999" title="My Book" />
The following example declares ns as an XML namespace prefix. It then uses the prefix of the namespace to create an XML literal and displays the element's final form.
' Place Imports statements at the top of your program. Imports <xmlns:ns="http://SomeNamespace"> Class TestClass1 Shared Sub TestPrefix() ' Create test using a global XML namespace prefix. Dim inner2 = <ns:inner2/> Dim test = _ <ns:outer> <ns:middle xmlns:ns="http://NewNamespace"> <ns:inner1/> <%= inner2 %> </ns:middle> </ns:outer> ' Display test to see its final form. Console.WriteLine(test) End Sub End Class
This code displays the following text:
<ns:outer xmlns:ns="http://SomeNamespace">
<ns:middle xmlns:ns="http://NewNamespace">
<ns:inner1 />
<inner2 xmlns="http://SomeNamespace" />
</ns:middle>
</ns:outer>
Notice that the compiler converted the prefix of the global XML namespace into a prefix definition for the XML namespace. The <ns:middle> element redefines the XML namespace prefix for the <ns:inner1> element. However, the <ns:inner2> element uses the namespace defined by the Imports statement.
Note: