VB2012读取xml

上回谢了生成写xml的,现在把读取的补上

文件如下

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<JLVersion>
    <version>
    <Number>jl-11.0104</Number>
    <Time>2011-01-04</Time>
    </version>
</JLVersion>

读取的程序卸载一个winform程序内

        Dim xmldoc As New XmlDocument
        Dim str As String = String.Empty
        '获取物理路径
        Dim path As String = "D:JLVersion.xml"
        '加载xml文档...
        xmldoc.Load(path)
        '第一个节点
        Dim rootnode As XmlNode = xmldoc.SelectSingleNode("JLVersion")
        '遍历子节点
        For Each node As XmlNode In rootnode
            str = str + node.ChildNodes(0).InnerText
            str = str + node.ChildNodes(1).InnerText
        Next

        Label1.Text = str

更复杂的例子就不贴了

Dim node As XmlNode = rootnode.ChildNodes(0)

这句拿到根节点的第一个子节点

node.Attributes(0).Name 

拿到第一个属性的名字

node.Attributes(0).Value

拿到第一个属性的值

比如<student ID="1">

属性的名字是ID,属性的值是1

node.InnerText

如果此节点下只有文本了,比如<student>xxx</student>会输出xxx

但如果此节点下还有根节点,比如

  <student ID="1">
    <user>
      <username>
        <english>hahaha</english>
        <chinese>gagaga</chinese>
      </username>
      <password>password01</password>
    </user>
  </student>
  <student ID="2">
    <user>
      <username>username02</username>
      <password>password02</password>
    </user>
  </student>

他会输出所有子节点下的文本,输出是hahahagagagapassword01

原文地址:https://www.cnblogs.com/mihe/p/3723651.html