Literal de elemento XML (Visual Basic)
A literal that represents an XElement object.
<name [ attributeList ] />
-or-
<name [ attributeList ] > [ elementContents ] </[ name ]>
Parts
Term |
Definition |
< |
Required. Opens the starting element tag. |
name |
Required. Name of the element. The format is one of the following:
PartDescription
ePrefix Optional.XML namespace prefix for the element.Must be a global XML namespace that is defined with an Imports statement in the file or at the project level, or a local XML namespace that is defined in this element or a parent element.
eName Required.Name of the element.The format is one of the following:
|
attributeList |
Optional. List of attributes declared in the literal. attribute [ attribute ... ] Each attribute has one of the following syntaxes:
PartDescription
aPrefix Optional.XML namespace prefix for the attribute.Must be a global XML namespace that is defined with an Imports statement, or a local XML namespace that is defined in this element or a parent element.
aName Required.Name of the attribute.The format is one of the following:
aValue Optional.Value of the attribute.The format is one of the following:
|
/> |
Optional. Indicates that the element is an empty element, without content. |
> |
Required. Ends the beginning or empty element tag. |
elementContents |
Optional. Content of the element. content [ content ... ] Each content can be one of the following:
|
</[name]> |
Optional. Represents the closing tag for the element. The optional name parameter is not allowed when it is the result of an embedded expression. |
Valor de retorno
An XElement object.
Comentários
You can use the XML element literal syntax to create XElement objects in your code.
Observação |
---|
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 Expressões inseridas no XML (Visual Basic).
O Visual Basic compilador converte o elemento XML literal em chamadas para o XElement construtor e, se for necessária, o XAttribute construtor.
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 Instrução Imports (Namespace XML).
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.
Exemplo
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 = "book"
Dim authorName = "My Author"
Dim attributeName1 = "year"
Dim attributeValue1 = 1999
Dim attributeName2 = "title"
Dim attributeValue2 = "My Book"
Dim book As XElement =
<<%= elementType %>
isbn="1234"
author=<%= authorName %>
<%= attributeName1 %>=<%= attributeValue1 %>
<%= New XAttribute(attributeName2, attributeValue2) %>
/>
Console.WriteLine(book)
This code displays the following text:
<book isbn="1234" author="My Author" 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.
Consulte também
Referência
Literal de comentário XML (Visual Basic)
literal CDATA XML (Visual Basic)
Instrução Imports (Namespace XML)
Conceitos
Nomes de elementos e atributos XML declarados (Visual Basic)
Expressões inseridas no XML (Visual Basic)