如何:使用 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 MainModule1之后添加以下代码。

    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>
    

另请参阅