如何:使用 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 代码。 将以下代码添加到 Module1 模块的 Sub Main。 此代码将源 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 查询创建的,该查询返回一系列 XElement 对象,其中包含转换后的 HTML。 可以使用嵌入的表达式将源文档中的值输入新的 XML 格式。

    使用 Save 方法将生成的 HTML 文档写入文件。

    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. Module1Sub Main 后,添加新方法 (Sub),将 <Description> 节点转换为指定的 HTML 格式。 此方法由上一步中的代码调用,用于保留 <Description> 元素的格式。

    此方法将 <Description> 元素的子元素替换为 HTML。 ReplaceWith 方法用于保留子元素的位置。 <Description> 元素的转换后内容包含在 HTML 段落 (<p>) 元素中。 Nodes 属性用于检索 <Description> 元素的转换后内容。 这可确保子元素包含在转换后的内容中。

    Module1Sub Main 之后添加以下代码。

    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>
    

另请参阅