如何:使用 LINQ 转换 XML (Visual Basic)
更新:2007 年 11 月
XML 文本使您可以轻松地从一个源读取 XML,并将其转换为新的 XML 格式。您可以利用 LINQ 查询检索要转换的内容,或将现有文档中的内容更改为新的 XML 格式。
本主题中的示例将 XML 源文档的内容转换为可在浏览器中查看的 HTML。
说明: |
---|
对于在以下说明中使用的某些 Visual Studio 用户界面元素,您的计算机可能会显示不同的名称或位置。这些元素取决于您使用的 Visual Studio 版本及设置。有关更多信息,请参见Visual Studio 设置。 |
转换 XML 文档
在 Visual Studio 中,在“控制台应用程序”项目模板中创建一个新的 Visual Basic 项目。
双击在项目中创建的 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 文档的代码后,添加下面的代码,以便从对象检索所有 <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")
在 Module1 的 Sub Main 之后,添加一个新方法 (Sub),将 <Description> 节点转换为指定 HTML 格式。此方法由上一步中的代码调用,用于保留 <Description> 元素的格式。
此方法将 <Description> 元素的子元素替换为 HTML。ReplaceWith 方法用于保留子元素的位置。<Description> 元素的转换内容包含在 HTML 段落 (<p>) 元素中。Nodes 属性用于检索 <Description> 元素的转换内容。这样确保子元素包括在转换内容中。
在 Module1 的 Sub 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
保存更改。
按 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>
请参见
任务
如何:从文件、字符串或流加载 XML (Visual Basic)