共用方式為


如何:使用 LINQ 轉換 XML (Visual Basic)

XML 常值 可讓您輕鬆地從一個來源讀取 XML,並將其轉換成新的 XML 格式。 您可以利用 LINQ 查詢來擷取要轉換的內容,或將現有文件中的內容變更為新的 XML 格式。

本主題中的範例會將內容從 XML 源文檔轉換成 HTML,以在瀏覽器中檢視。

備註

您的電腦可能會在下列指示中顯示某些 Visual Studio 使用者介面元素的不同名稱或位置。 您擁有的 Visual Studio 版本,以及您所使用的設定會決定這些元素。 如需詳細資訊,請參閱 個人化 IDE

轉換 XML 檔

  1. 在 Visual Studio 中,在 控制台應用程式 專案範本中建立新的 Visual Basic 專案。

  2. 按兩下專案建立的Module1.vb檔案,以修改Visual Basic程式碼。 將下列程式代碼新增至 Sub Main 模組的 Module1 。 此程式代碼會將來源 XML 檔建立為 XDocument 物件。

    Dim catalog =
      <?xml version="1.0"?>
        <Catalog>
          <Book id="bk101">
            <Author>Garghentini, Davide</Author>
            <Title>XML Developer's Guide</Title>
            <Price>44.95</Price>
            <Description>
              An in-depth look at creating applications
              with <technology>XML</technology>. For
              <audience>beginners</audience> or
              <audience>advanced</audience> developers.
            </Description>
          </Book>
          <Book id="bk331">
            <Author>Spencer, Phil</Author>
            <Title>Developing Applications with Visual Basic .NET</Title>
            <Price>45.95</Price>
            <Description>
              Get the expert insights, practical code samples,
              and best practices you need
              to advance your expertise with <technology>Visual
              Basic .NET</technology>.
              Learn how to create faster, more reliable applications
              based on professional,
              pragmatic guidance by today's top <audience>developers</audience>.
            </Description>
          </Book>
        </Catalog>
    

    如何:從檔案、字串或數據流載入 XML

  3. 在建立原始 XML 文件的程式碼後,加入以下程式碼,從物件中擷取所有 <Book> 元素並轉換成 HTML 文件。 元素清單 <Book> 是透過使用 LINQ 查詢建立的,該查詢回傳包含轉換後 HTML 的物件集合 XElement 。 您可以使用內嵌表示式,以新的 XML 格式放置源文件的值。

    產生的 HTML 檔會使用 Save 方法寫入檔案。

    Dim htmlOutput =
      <html>
        <body>
          <%= From book In catalog.<Catalog>.<Book>
              Select <div>
                       <h1><%= book.<Title>.Value %></h1>
                       <h3><%= "By " & book.<Author>.Value %></h3>
                        <h3><%= "Price = " & book.<Price>.Value %></h3>
                        <h2>Description</h2>
                        <%= TransformDescription(book.<Description>(0)) %>
                        <hr/>
                      </div> %>
        </body>
      </html>
    
    htmlOutput.Save("BookDescription.html")
    
  4. Sub MainModule1 之後,新增一個方法(Sub)將 <Description> 節點轉換成指定的 HTML 格式。 此方法由前一步的程式碼呼叫,用以保留元素格式 <Description>

    此方法將元素的 <Description> 子元素替換為 HTML。 方法 ReplaceWith 可用來保留子專案的位置。 元素轉換後的內容 <Description> 會包含在 HTML 段落(<p>)元素中。 該 Nodes 性質用於檢索該元素的轉換後內容 <Description> 。 這可確保轉換的內容中包含子元素。

    Sub Main之後新增下列程式碼於Module1

    Public Function TransformDescription(ByVal desc As XElement) As XElement
    
      ' Replace <technology> elements with <b>.
      Dim content = (From element In desc...<technology>).ToList()
    
      If content.Count > 0 Then
        For i = 0 To content.Count - 1
          content(i).ReplaceWith(<b><%= content(i).Value %></b>)
        Next
      End If
    
      ' Replace <audience> elements with <i>.
      content = (From element In desc...<audience>).ToList()
    
      If content.Count > 0 Then
        For i = 0 To content.Count - 1
          content(i).ReplaceWith(<i><%= content(i).Value %></i>)
        Next
      End If
    
      ' Return the updated contents of the <Description> element.
      Return <p><%= desc.Nodes %></p>
    End Function
    
  5. 儲存您的變更。

  6. 按 F5 執行程式碼。 產生的已儲存檔如下所示:

    <?xml version="1.0"?>
    <html>
      <body>
        <div>
          <h1>XML Developer's Guide</h1>
          <h3>By Garghentini, Davide</h3>
          <h3>Price = 44.95</h3>
          <h2>Description</h2>
          <p>
            An in-depth look at creating applications
            with <b>XML</b>. For
            <i>beginners</i> or
            <i>advanced</i> developers.
          </p>
          <hr />
        </div>
        <div>
          <h1>Developing Applications with Visual Basic .NET</h1>
          <h3>By Spencer, Phil</h3>
          <h3>Price = 45.95</h3>
          <h2>Description</h2>
          <p>
            Get the expert insights, practical code
            samples, and best practices you need
            to advance your expertise with <b>Visual
            Basic .NET</b>. Learn how to create faster,
            more reliable applications based on
            professional, pragmatic guidance by today's
            top <i>developers</i>.
          </p>
          <hr />
        </div>
      </body>
    </html>
    

另請參閱