[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

 1 Imports System.IO
 2 Imports System.Xml
 3 Module ParsingUsingXmlTextReader
 4 Sub Main()
 5   Dim m_xmlr As XmlTextReader
 6   ‘Create the XML Reader
 7   m_xmlr = New XmlTextReader("C:\Personal\family.xml")
 8   ‘Disable whitespace so that you don‘t have to read over whitespaces
 9   m_xmlr.WhiteSpaceHandling = WhiteSpaceHandling.NONE
10   ‘read the xml declaration and advance to family tag
11   m_xmlr.Read()
12   ‘read the family tag
13   m_xmlr.Read()
14   ‘Load the Loop
15   While Not m_xmlr.EOF
16     ‘Go to the name tag
17     m_xmlr.Read()
18     ‘if not start element exit while loop
19     If Not m_xmlr.IsStartElement() Then
20       Exit While
21     End If
22     ‘Get the Gender Attribute Value
23     Dim genderAttribute = m_xmlr.GetAttribute("gender")
24     ‘Read elements firstname and lastname
25     m_xmlr.Read()
26     ‘Get the firstName Element Value
27     Dim firstNameValue = m_xmlr.ReadElementString("firstname")
28     ‘Get the lastName Element Value
29     Dim lastNameValue = m_xmlr.ReadElementString("lastname")
30     ‘Write Result to the Console
31     Console.WriteLine("Gender: " & genderAttribute _
32       & " FirstName: " & firstNameValue & " LastName: " _
33       & lastNameValue)
34     Console.Write(vbCrLf)
35   End While
36   ‘close the reader
37   m_xmlr.Close()
38 End Sub
39 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

 1 Imports System.IO
 2 Imports System.Xml
 3 Module ParsingUsingXmlDocument
 4 Sub Main()
 5   Try
 6     Dim m_xmld As XmlDocument
 7     Dim m_nodelist As XmlNodeList
 8     Dim m_node As XmlNode
 9     ‘Create the XML Document
10     m_xmld = New XmlDocument()
11     ‘Load the Xml file
12     m_xmld.Load("C:\CMS\Personal\family.xml")
13     ‘Get the list of name nodes
14     m_nodelist = m_xmld.SelectNodes("/family/name")
15     ‘Loop through the nodes
16     For Each m_node In m_nodelist
17       ‘Get the Gender Attribute Value
18       Dim genderAttribute = m_node.Attributes.GetNamedItem("gender").Value
19       ‘Get the firstName Element Value
20       Dim firstNameValue = m_node.ChildNodes.Item(0).InnerText
21       ‘Get the lastName Element Value
22       Dim lastNameValue = m_node.ChildNodes.Item(1).InnerText
23       ‘Write Result to the Console
24       Console.Write("Gender: " & genderAttribute _
25         & " FirstName: " & firstNameValue & " LastName: " _
26         & lastNameValue)
27       Console.Write(vbCrLf)
28     Next
29   Catch errorVariable As Exception
30     ‘Error trapping
31     Console.Write(errorVariable.ToString())
32   End Try
33 End Sub
34 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

时间: 2024-12-06 03:24:37

[vb.net]XML File Parsing in VB.NET的相关文章

Android项目部署时,发生AndroidRuntime:android.view.InflateException: Binary XML file line #168: Error inflating class错误

这个错误也是让我纠结了一天,当时写的项目在安卓虚拟机上运行都很正常,于是当我部署到安卓手机上时,点击登陆按钮跳转到用户主界面的时候直接结束运行返回登陆界面.    当时,我仔细检查了一下自己的代码,并没有发现什么问题,在logcat上显示的报错如下:AndroidRuntime:android.view.InflateException: Binary XML file line #168: Error inflating class(这是其中报错的最主要的一行信息).  于是我在百度上几乎查看

bug_ _图片_android.view.InflateException: Binary XML file line #1: Error inflating class &lt;unknown&gt;

=========== 1   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zgan.community/com.zgan.community.activity.CommunityPolicitalDetailActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class <unknow

android Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Must specify unique

今天写了一个静态得fragment,好久没写了,一写就出现问题了,先看下布局文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height=&qu

从smack-config.xml文件中加载文件内容 Loads the configuration from the smack-config.xml file

/** * Loads the configuration from the smack-config.xml file.<p> * * So far this means that: * 1) a set of classes will be loaded in order to execute their static init block * 2) retrieve and set the current Smack release */ static { try { // Get an

android 细节之android.view.InflateException: Binary XML file line #95: Error inflating class(out of m)

今天的异常很有意思,叫做android.view.InflateException: Binary XML file line #95: Error inflating class(out of memory) . 其实是因为out of memory,导致 xml是不可能被充气成功,因此activity的onCreate方法中, setContentView(R.layout.***)也就不可能成功调用. 他出现在我有多个教学动画,并且播放的动画,是基于imageView,imageView的

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/AndroidRuntime(6936): android.view.InflateException: Binary XML file line #2: Error inflating class com.example.FileListItem 06-27 14:29:27.600: E/AndroidRuntime(

Binary XML file line #7: Error inflating class fragment

这几天一直在学习碎片,想自己写一个相关的程序试试,没想到刚写一点就出了问题. 在加载主布局文件activity_main.xml时候,出现错误 06-12 13:11:12.873: E/AndroidRuntime(2022): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gentleni.demo_grideview_002/com.gentleni.demo_grideview_002.Mai

ADT开发AndroidManifest.xml file missing错误

一个错误“AndroidManifest.xml file missing”但helloworld目录下有此文件,几番google仍没能解决.想起曾经在网络上看到的一个修复project的办法,抱着死马当 活马医的态度:右击helloworld"->"Android Tools"->"Fix Project Properties" 参考 http://blog.chinaunix.net/uid-20718037-id-3791492.html

java.lang.IllegalArgument,Parse error in application web.xml file at jndi:/localhost/WEB-INF/web.xml

启动Tomcat时错误:java.lang.IllegalArgumentException: Can't convert argument: nullParse error in application web.xml file at jndi:/localhost/WEB-INF/web.xml Resolution:上述问题,是由于在Eclipse下重构JEE项目名时,Eclipse自动更新了部署文件web.xml,重新生成了xml文件的头部声明,新增加了javaee的命名空间. Xml代