前面已经学习了添加,删除和修改XML文件的某个节点,这一章就介绍查询所有的XML节点。
1.首先还是得定义读取XML节点的变量:
Dim xmlDoc as New XmlDocument
xmlDoc.Load("bookstore.xml")
2.定义nodelist,用于读取根节点的所有子节点:
Dim xNodeList as XmlNodeList =xmlDoc.SelectSingleNode("bookstore").ChildNodes
3.读取所有的book节点
Dim xNode as xmlNode
For Each xNode In xNodeList //寻找book子节点
Dim xElement as XmlElement =CType(xNode,XmlElement)
Console.Write(xElement.GetAttribute("genre")) //读取book的两个属性genre,ISBN
Console.Write(xElement.GetAttribute("ISBN"))
Dim xSubNodeList as XmlNodeList =xElement.ChildNodes //用于读取book的所有子节点
Dim xSubNode as XmlNode
For Each xSubNode in xSubNodeList //寻找子节点title,author,price
Dim xSubElement as XmlElement =CType(xSubNode,XmlElement)
Console.write(xSubElement.InnerText()) //读取子节点与读取属性的方法不同
Next
Next
好了,xml文件的所有节点的属性与子节点都显示出来了。