Required. Name of the element. The format is one of the following:
Literal text for the element name, of the form [ePrefix:]eName, where:
Part
Description
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:
- Literal text. See Names of Declared XML Elements and Attributes. - Embedded expression of the form <%= eNameExp %>. The type of eNameExp must be String or a type that is implicitly convertible to XName.
Embedded expression of the form <%= nameExp %>. The type of nameExp must be String or a type implicitly convertible to XName. An embedded expression is not allowed in a closing tag of an element.
attributeList
Optional. List of attributes declared in the literal.
attribute [ attribute ... ]
Each attribute has one of the following syntaxes:
Attribute assignment, of the form [aPrefix:]aName=aValue, where:
Part
Description
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:
- Literal text. See Names of Declared XML Elements and Attributes. - Embedded expression of the form <%= aNameExp %>. The type of aNameExp must be String or a type that is implicitly convertible to XName.
aValue
Optional. Value of the attribute. The format is one of the following:
- Literal text, enclosed in quotation marks. - Embedded expression of the form <%= aValueExp %>. Any type is allowed.
Embedded expression of the form <%= aExp %>.
/>
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:
Literal text. All the white space in elementContents becomes significant if there is any literal text.
Embedded expression of the form <%= contentExp %>.
You can use the XML element literal syntax to create XElement objects in your code.
Piezīme
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 (XML Namespace).
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.
Example 1
The following example shows how to create a simple XML element that has two nested empty elements.
VB
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.
XML
<outer><inner1></inner1><inner2 /></outer>
Example 2
The following example shows how to use embedded expressions to name an element and create attributes.
VB
Dim elementType = "book"Dim authorName = "My Author"Dim attributeName1 = "year"Dim attributeValue1 = 1999Dim attributeName2 = "title"Dim attributeValue2 = "My Book"Dim book As XElement =
<<%= elementType %>
isbn="1234"
author=<%= authorName %>
<%= attributeName1 %>=<%= attributeValue1 %>
<%= New XAttribute(attributeName2, attributeValue2) %>
/>
Console.WriteLine(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.
VB
' Place Imports statements at the top of your program. Imports <xmlns:ns="http://SomeNamespace">
Class TestClass1
SharedSub 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)
EndSubEndClass
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.
Šī satura avotu var atrast vietnē GitHub, kur varat arī izveidot un pārskatīt problēmas un atgādāšanas pieprasījumus. Lai iegūtu papildinformāciju, skatiet mūsu līdzstrādnieku rokasgrāmatu.
.NET atsauksmes
.NET ir atklātā pirmkoda projekts. Atlasiet saiti, lai sniegtu atsauksmes:
Pievienojieties meetup sērijai, lai kopā ar citiem izstrādātājiem un ekspertiem izveidotu mērogojamus AI risinājumus, kuru pamatā ir reālas lietošanas gadījumi.