65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 DOM 在 VB.NET 中解析 XML 文件

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.80/5 (2投票s)

2011年6月12日

CPOL

1分钟阅读

viewsIcon

42674

downloadIcon

1299

使用 DOM 在 VB.NET 中解析 XML 文件

引言

互联网和 CodeProject 上有很多 XML 解析器/读取器(包括 我自己的 C/C++ 版本)。这个版本是几年前用 VB 6.0 编写的,基于 DOM(文档对象模型)。我几乎忘记了它,直到最近我决定将 VB 6.0 项目升级(或查看是否可以轻松升级)到 VB 2008(VB 2010 无法识别 VB 6.0 项目的文件)。我已经有了一个非常耗时的项目升级经验,该项目包含许多绘图和数据库相关函数和数组,因为它需要你手动重写旧代码。
XML 解析器没有绘图函数或 VB6 数组,并自动升级到 VB.NET,尽管生成了与对象默认属性相关的警告。我从代码中删除了这些行,但对于感兴趣的人,我将 _UpgradeReport.htm 保留在项目中。

Using the Code

与代码示例一起提供的 XML 文件是 MSXML 4.0 SDK 中的 stocks.xml 文件

示例 XML 文件

虽然 VB 6.0 和 VB.NET 是不同的语言,但它们仍然有很多共同之处,为了这次实验升级的公平性,我没有更改或更正代码中的任何内容。因此,例如,你可以看到旧式的错误处理方式,而不是 Try-Catch 块。

'---1.Load an xml document into a DOM instance----------------
  oXMLDom.async = False
  oXMLDom.validateOnParse = False
  oXMLDom.resolveExternals = False
  oXMLDom.preserveWhiteSpace = True
  fileName = txtFileName.Text

  If oXMLDom.load(My.Application.Info.DirectoryPath & "\" & fileName) = False Then

   ErrorMessage = "Error message: failed to load XML data from file." _
            & vbCrLf & "Check the file name."

   Response = MsgBox(ErrorMessage, StyleWarning)
   'MsgBox "Error: failed to load XML data from file. Check the file name."
  End If

  '-----2.Get the root node and print its name---------------------
  'get root element:
  root = oXMLDom.documentElement
  'output the name of root node

  strout = "Root node: " & root.nodeName & vbCrLf & vbCrLf

  txtOutput.Text = strout
  '----------------
  'Create an IXMLDOMNodeList object by using the xml document's
  'getElementsByTagName method, and examine the content of each node.

  '------3.Output the elements with values, parent nodes and attributes---------------
  'output "Items" nodes:
  objNodeList = oXMLDom.getElementsByTagName(" ")
  'strout = "The node list has " & objNodeList.length & " items." & vbCrLf
  Dim Attrib As Object
  '"objNodeList" index starts from 0:
  For i = 1 To (objNodeList.length)

   'Output only elements:

   If objNodeList.item(i - 1).nodeType = MSXML2.tagDOMNodeType.NODE_ELEMENT Then
    n = n + 1 'element node's counter

    '"item" index starts from 0
       strout = strout & vbCrLf & CStr(n) & vbCrLf & "Type: " _
    & objNodeList.item(i - 1).nodeTypeString & vbCrLf & "Node: " _
           & objNodeList.item(i - 1).nodeName & vbCrLf & "Text: " _
    & objNodeList.item(i).text & vbCrLf & "Parent node: " _
           & objNodeList.item(i - 1).parentNode.nodeName & vbCrLf _
    & "Child nodes: " & objNodeList.item(i - 1).childNodes.length _
           & vbCrLf & vbCrLf

    For Each Attrib In objNodeList.item(i - 1).attributes

     strout = strout & "Attribute type: " & Attrib.nodeTypeString & _
    vbCrLf & "Attribute name: " _
         & Attrib.Name & vbCrLf & "Value: " & Attrib.nodeValue & vbCrLf
    Next Attrib

   End If
  Next  

这是解析的输出

解析 stocks.xml 文件的输出

请注意,要运行可执行文件,必须将生成的 Interop.MSXML2.dll 文件放在 .exe 文件所在的目录中。

参考文献

历史

  • 2009 年 8 月 - 在 Visual Basic 6.0 中构建
  • 2011 年 6 月 12 日 - 在 Visual Basic 2008 中重新编译
© . All rights reserved.