XML 常值概觀 (Visual Basic)XML Literals Overview (Visual Basic)

Xml 常值可讓您將 xml 直接併入 Visual Basic 的程式碼中。An XML literal allows you to incorporate XML directly into your Visual Basic code. XML 常值語法代表 LINQ to XMLLINQ to XML 物件,而且類似于 XML 1.0 語法。The XML literal syntax represents LINQ to XMLLINQ to XML objects, and it is the similar to the XML 1.0 syntax. 這可讓您更輕鬆地以程式設計方式建立 XML 元素和檔,因為您的程式碼與最終 XML 的結構相同。This makes it easier to create XML elements and documents programmatically because your code has the same structure as the final XML.

Visual Basic 會將 XML 常值編譯成 LINQ to XMLLINQ to XML 物件。Visual Basic compiles XML literals into LINQ to XMLLINQ to XML objects. LINQ to XMLLINQ to XML提供簡單的物件模型來建立和管理 XML,而此模型與語言整合式查詢(LINQ)緊密整合。provides a simple object model for creating and manipulating XML, and this model integrates well with Language-Integrated Query (LINQ). 如需詳細資訊,請參閱 XElementFor more information, see XElement.

您可以將 Visual Basic 運算式內嵌在 XML 常值中。You can embed a Visual Basic expression in an XML literal. 在執行時間,您的應用程式會 LINQ to XMLLINQ to XML 為每個常值建立一個物件,並併入內嵌運算式的值。At run time, your application creates a LINQ to XMLLINQ to XML object for each literal, incorporating the values of the embedded expressions. 這可讓您指定 XML 常值內的動態內容。This lets you specify dynamic content inside an XML literal. 如需詳細資訊,請參閱XML 中的內嵌運算式For more information, see Embedded Expressions in XML.

如需 XML 常值語法與 XML 1.0 語法之間差異的詳細資訊,請參閱Xml 常值和 xml 1.0 規格For more information about the differences between the XML literal syntax and the XML 1.0 syntax, see XML Literals and the XML 1.0 Specification.

簡單常值Simple Literals

您可以 LINQ to XMLLINQ to XML 輸入或貼上有效的 XML,在 Visual Basic 程式碼中建立物件。You can create a LINQ to XMLLINQ to XML object in your Visual Basic code by typing or pasting in valid XML. XML 元素常值會傳回 XElement 物件。An XML element literal returns an XElement object. 如需詳細資訊,請參閱Xml 元素常值和xml 常值和 xml 1.0 規格For more information, see XML Element Literal and XML Literals and the XML 1.0 Specification. 下列範例會建立具有數個子項目的 XML 專案。The following example creates an XML element that has several child elements.

Dim contact1 As XElement = 
    <contact>
      <name>Patrick Hines</name>
      <phone type="home">206-555-0144</phone>
      <phone type="work">425-555-0145</phone>
    </contact>

您可以使用來啟動 XML 常值,以建立 XML 檔 <?xml version="1.0"?> ,如下列範例所示。You can create an XML document by starting an XML literal with <?xml version="1.0"?>, as shown in the following example. XML 檔常值會傳回 XDocument 物件。An XML document literal returns an XDocument object. 如需詳細資訊,請參閱XML 檔常值。For more information, see XML Document Literal.

Dim contactDoc As XDocument = 
    <?xml version="1.0"?>
    <contact>
      <name>Patrick Hines</name>
      <phone type="home">206-555-0144</phone>
      <phone type="work">425-555-0145</phone>
    </contact>

注意

Visual Basic 中的 XML 常值語法與 XML 1.0 規格中的語法不相同。The XML literal syntax in Visual Basic is not identical to the syntax in the XML 1.0 specification. 如需詳細資訊,請參閱Xml 常值和 xml 1.0 規格For more information, see XML Literals and the XML 1.0 Specification.

行接續Line Continuation

XML 常值可以跨越多行,而不需要使用行接續字元(空格-底線-enter 順序)。An XML literal can span multiple lines without using line continuation characters (the space-underscore-enter sequence). 這可讓您更輕鬆地比較程式碼中的 XML 常值與 XML 檔。This makes it easier to compare XML literals in code with XML documents.

編譯器會將行接續字元視為 XML 常值的一部分。The compiler treats line continuation characters as part of an XML literal. 因此,只有當它屬於物件時,您才應該使用空格鍵-底線-enter 序列 LINQ to XMLLINQ to XMLTherefore, you should use the space-underscore-enter sequence only when it belongs in the LINQ to XMLLINQ to XML object.

不過,如果內嵌運算式中有多行運算式,您就需要行接續字元。However, you do need line continuation characters if you have a multiline expression in an embedded expression. 如需詳細資訊,請參閱XML 中的內嵌運算式For more information, see Embedded Expressions in XML.

在 XML 常值中嵌入查詢Embedding Queries in XML Literals

您可以使用內嵌運算式中的查詢。You can use a query in an embedded expression. 當您執行這項操作時,查詢所傳回的元素會加入至 XML 元素。When you do this, the elements returned by the query are added to the XML element. 這可讓您將動態內容(例如使用者查詢的結果)新增至 XML 常值。This lets you add dynamic content, such as the result of a user's query, to an XML literal.

例如,下列程式碼會使用內嵌查詢,從陣列的成員建立 XML 專案 phoneNumbers2 ,然後將這些專案當做的子系加入 contact2For example, the following code uses an embedded query to create XML elements from the members of the phoneNumbers2 array and then add those elements as children of contact2.

Public Class XmlSamples

  Public Sub Main()
    ' Initialize the objects. 

    Dim phoneNumbers2 As Phone() = { 
        New Phone("home", "206-555-0144"), 
        New Phone("work", "425-555-0145")}

    ' Convert the data contained in phoneNumbers2 to XML. 

    Dim contact2 = 
        <contact>
          <name>Patrick Hines</name>
          <%= From p In phoneNumbers2 
            Select <phone type=<%= p.Type %>><%= p.Number %></phone> 
          %>
        </contact>

    Console.WriteLine(contact2)
  End Sub

End Class

Class Phone
  Public Type As String
  Public Number As String
  Public Sub New(ByVal t As String, ByVal n As String)
    Type = t
    Number = n
  End Sub
End Class

編譯器如何從 XML 常值建立物件How the Compiler Creates Objects from XML Literals

Visual Basic 編譯器會將 XML 常值轉譯為對等的函式的呼叫, LINQ to XMLLINQ to XML 以建立 LINQ to XMLLINQ to XML 物件。The Visual Basic compiler translates XML literals into calls to the equivalent LINQ to XMLLINQ to XML constructors to build up the LINQ to XMLLINQ to XML object. 例如,Visual Basic 編譯器會將下列程式碼範例轉譯為 XML 版本指令的函式呼叫 XProcessingInstruction 、呼叫、和專案的函式, XElement <contact> <name> <phone> 以及呼叫 XAttribute 屬性的 type 函式。For example, the Visual Basic compiler will translate the following code example into a call to the XProcessingInstruction constructor for the XML version instruction, calls to the XElement constructor for the <contact>, <name>, and <phone> elements, and calls to the XAttribute constructor for the type attribute. 具體而言,假設下列範例中的屬性,Visual Basic 編譯器會呼叫兩次此函式 XAttribute(XName, Object)Specifically, given the attributes in the following sample, the Visual Basic compiler will call the XAttribute(XName, Object) constructor twice. 第一個會傳遞參數的值 type name 和參數的值 home valueThe first will pass the value type for the name parameter and the value home for the value parameter. 第二個也會傳遞 type 參數的值 name ,但參數的值為 work valueThe second will also pass the value type for the name parameter, but the value work for the value parameter.

Dim contactDoc As XDocument = 
    <?xml version="1.0"?>
    <contact>
      <name>Patrick Hines</name>
      <phone type="home">206-555-0144</phone>
      <phone type="work">425-555-0145</phone>
    </contact>

另請參閱See also