[vb.net]XML File Parsing in VB.NET
Introduction
Parsing XML files has always been time consuming and sometimes tricky. .NET framework provides powerful new ways of parsing XML. The various techniques know to parse xml files with .NET framework are using XmlTextReader
, XmlDocument
, XmlSerializer
, DataSet
and XpathDocument
. I will explore the XmlTextReader
and XmlDocument
approach here.
The Xml File
Figure 1 outlines the xml file that will be parsed.
<?xml version="1.0" encoding="UTF-8"?>
<family>
<name gender="Male">
<firstname>Tom</firstname>
<lastname>Smith</lastname>
</name>
<name gender="Female">
<firstname>Dale</firstname>
<lastname>Smith</lastname>
</name>
</family>
Parsing XML with XMLTextReader
Using XmlTextReader
is appropriate when the structure of the XML file is relatively simple. Parsing with XmlTextReader
gives you a pre .net feel as you sequentially walk through the file using Read()
and get data using GetAttribute()
andReadElementString()
methods. Thus while using XmlTextReader
it is up to the developer to keep track where he is in the Xml file and Read()
correctly. Figure 2 below outlines parsing of xml file with XmlTextReader
Imports System.IO
Imports System.Xml
Module ParsingUsingXmlTextReader
Sub Main()
Dim m_xmlr As XmlTextReader
'Create the XML Reader
m_xmlr = New XmlTextReader("C:\Personal\family.xml")
'Disable whitespace so that you don't have to read over whitespaces
m_xmlr.WhiteSpaceHandling = WhiteSpaceHandling.NONE
'read the xml declaration and advance to family tag
m_xmlr.Read()
'read the family tag
m_xmlr.Read()
'Load the Loop
While Not m_xmlr.EOF
'Go to the name tag
m_xmlr.Read()
'if not start element exit while loop
If Not m_xmlr.IsStartElement() Then
Exit While
End If
'Get the Gender Attribute Value
Dim genderAttribute = m_xmlr.GetAttribute("gender")
'Read elements firstname and lastname
m_xmlr.Read()
'Get the firstName Element Value
Dim firstNameValue = m_xmlr.ReadElementString("firstname")
'Get the lastName Element Value
Dim lastNameValue = m_xmlr.ReadElementString("lastname")
'Write Result to the Console
Console.WriteLine("Gender: " & genderAttribute _
& " FirstName: " & firstNameValue & " LastName: " _
& lastNameValue)
Console.Write(vbCrLf)
End While
'close the reader
m_xmlr.Close()
End Sub
End Module
Parsing XML with XmlDocument
The XmlDocument
class is modeled based on Document Object Model. XmlDocument
class is appropriate if you need to extract data in a non-sequential manner. Figure 3 below outlines parsing of xml file with XmlDocument
Imports System.IO
Imports System.Xml
Module ParsingUsingXmlDocument
Sub Main()
Try
Dim m_xmld As XmlDocument
Dim m_nodelist As XmlNodeList
Dim m_node As XmlNode
'Create the XML Document
m_xmld = New XmlDocument()
'Load the Xml file
m_xmld.Load("C:\CMS\Personal\family.xml")
'Get the list of name nodes
m_nodelist = m_xmld.SelectNodes("/family/name")
'Loop through the nodes
For Each m_node In m_nodelist
'Get the Gender Attribute Value
Dim genderAttribute = m_node.Attributes.GetNamedItem("gender").Value
'Get the firstName Element Value
Dim firstNameValue = m_node.ChildNodes.Item().InnerText
'Get the lastName Element Value
Dim lastNameValue = m_node.ChildNodes.Item().InnerText
'Write Result to the Console
Console.Write("Gender: " & genderAttribute _
& " FirstName: " & firstNameValue & " LastName: " _
& lastNameValue)
Console.Write(vbCrLf)
Next
Catch errorVariable As Exception
'Error trapping
Console.Write(errorVariable.ToString())
End Try
End Sub
End Module
You will see the following result for both
Gender: Male FirstName: Tom LastName: Smith
Gender: Female FirstName: Dale LastName: Smith
http://www.codeproject.com/Articles/4826/XML-File-Parsing-in-VB-NET
最新文章
- Java Netty 4.x 用户指南
- 移动端H5页面高清多屏适配方案
- MongoDB 数据分发
- session保存用户登录
- 7.1SportsStore:Navigation and Checkout
- ural 1114,计数dp
- BZOJ 1630/2023 Ant Counting 数蚂蚁
- ASP.NET MVC5 高级编程 第2章 控制器
- oracle修改process和session数
- UVa 11488 - Hyper Prefix Sets
- js 数组的一些基本操作
- TensorFlow4Delphi
- SQL外连接
- topcoder srm 610 div1
- hive笔记:转义字符的使用
- 重新学习Servlet二
- Linux使用netstat命令查看并发连接数
- leaflet入门(二)GeoJson
- POJ 2570 线段树
- Java RMI 的使用及原理
热门文章
- bootstrap-面包屑和分页
- CRM合并事件
- (LinkedList) Remove Linked List Elements
- XE6移动开发环境搭建之IOS篇(2):安装VM9虚拟机(有图有真相)
- oracle 利用flashback将备库激活为read wirte(10g 及上)
- outlook找不到文件Outlook.pst 如何启动
- Mac后台开发MNMP(nginx , mysql, php)标配
- C# 自定义序列化问题
- 深入学习微框架:Spring Boot(转)
- php关闭错误提示