[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
[vb.net]XML File Parsing in VB.NET的更多相关文章
- ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id
出现场景:当点击"分类"再返回"首页"时,发生error退出 BUG描述:Caused by: java.lang.IllegalArgumentExcep ...
- Binary XML file line #2: Error inflating
06-27 14:29:27.600: E/AndroidRuntime(6936): FATAL EXCEPTION: main 06-27 14:29:27.600: E/AndroidRunti ...
- Android项目部署时,发生AndroidRuntime:android.view.InflateException: Binary XML file line #168: Error inflating class错误
这个错误也是让我纠结了一天,当时写的项目在安卓虚拟机上运行都很正常,于是当我部署到安卓手机上时,点击登陆按钮跳转到用户主界面的时候直接结束运行返回登陆界面. 当时,我仔细检查了一下自己的代码,并 ...
- ADT开发AndroidManifest.xml file missing错误
一个错误“AndroidManifest.xml file missing”但helloworld目录下有此文件,几番google仍没能解决.想起曾经在网络上看到的一个修复project的办法,抱着死 ...
- SVN :This XML file does not appear to have any style information associated with it.
SVN :This XML file does not appear to have any style information associated with it. The document tr ...
- The AndroidManifest.xml File
manifest (船运的)载货清单 http://www.android-doc.com/guide/topics/manifest/manifest-intro.html Every applic ...
- bug_ _图片_android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
=========== 1 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zgan.communit ...
- bug_ _ android.view.InflateException: Binary XML file line #2: Error inflating class <unknown
========= 5.0 android异常“android.view.InflateException: Binary XML file line # : Error inflating ...
- How to: Read Object Data from an XML File
This example reads object data that was previously written to an XML file using the XmlSerializer cl ...
随机推荐
- 关于iscroll阻止浏览器默认动作
使用iscroll时,移动端遇到需要长按复制功能,但是iscroll屏蔽了浏览器默认事件,所以实现不了. 解决方案: myScroll = new IScroll('#wrapper',{ preve ...
- Codeforces 721C [dp][拓扑排序]
/* 题意:给你一个有向无环图.给一个限定t. 问从1点到n点,在不超过t的情况下,最多可以拜访几个点. 保证至少有一条路时限不超过t. 思路: 1.由无后向性我们可以知道(取决于该图是一个DAG), ...
- 通过SQL语句提取存储过程中的内容
首先,列出服务器上所有数据库. -- 获取数据库列表 SELECT name FROM master.dbo.sysdatabases ORDER BY name 其次,这是一种让所有的用户从数据库中 ...
- 数据库的索引和填充因子fillfactor
索引分为聚簇索引和非聚簇索引 1.聚簇索引/聚集索引 聚簇索引的顺序就是数据的物理存储顺序,对于一个表来说,只有一个聚簇索引 create unique clustered index id_inde ...
- 【转】nexus Maven 环境搭建
http://www.cnblogs.com/quanyongan/archive/2013/04/24/3037589.html 为什么要搭建nexus私服,原因很简单,有些公司都不提供外网给项目组 ...
- [vB.NET]为控件添加鼠标悬浮时的提示气泡
实例代码: Dim k As ToolTip k = New ToolTip() k.AutoPopDelay = '显示出气泡后的延时时间(毫秒) k.InitialDelay = '出现前的延时( ...
- activeamq启动失败
启动activeamq,启动时控制台显示: INFO: Using java '/usr/bin/java'INFO: Starting - inspect logfiles specified in ...
- 洛谷P3374 【模板】树状数组 1
P3374 [模板]树状数组 1 140通过 232提交 题目提供者HansBug 标签 难度普及/提高- 提交 讨论 题解 最新讨论 题目描述有误 题目描述 如题,已知一个数列,你需要进行下面两 ...
- AIDL学习
(转自)可以参见:http://www.2cto.com/kf/201406/312244.html 1.为什么要有AIDL? 无论学什么东西,最先得弄明白为什么要有这个东西,不要说存在即是合理,存在 ...
- mongodb转换List实体(去掉多余的字段) 批量添加
Gson gson = new Gson(); String str = "list集合json字符串"; JsonObject object = gson.fromJson(st ...