星空网 > 软件开发 > ASP.net

[vb.net]XML File Parsing in VB.NET

Introduction

Parsing , , , DataSet and XpathDocument. I will explore the and approach here.

The

Figure 1 outlines the

<??><family> <name gender="Male">  <firstname>Tom</firstname>  <lastname>Smith</lastname> </name> <name gender="Female">  <firstname>Dale</firstname>  <lastname>Smith</lastname> </name></family>

Parsing

Using is appropriate when the structure of the 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 it is up to the developer to keep track where he is in the Read() correctly. Figure 2 below outlines parsing of

 1 Imports System.IO 2 Imports System. 3 Module ParsingUsing 4 Sub Main() 5  Dim m_As  6  'Create the  7  m_New "C:\Personal\family.") 8  'Disable whitespace so that you don't have to read over whitespaces 9  m_ WhiteSpaceHandling.NONE10  'read the 11  m_12  'read the family tag13  m_14  'Load the Loop15  While Not m_16   'Go to the name tag17   m_18   'if not start element exit while loop19   If Not m_Then20    Exit While21   End If22   'Get the Gender Attribute Value23   Dim genderAttribute = m_"gender")24   'Read elements firstname and lastname25   m_26   'Get the firstName Element Value27   Dim firstNameValue = m_"firstname")28   'Get the lastName Element Value29   Dim lastNameValue = m_"lastname")30   'Write Result to the Console31   Console.WriteLine("Gender: " & genderAttribute _32    & " FirstName: " & firstNameValue & " LastName: " _33    & lastNameValue)34   Console.Write(vbCrLf)35  End While36  'close the reader37  m_38 End Sub39 End Module

Parsing

The class is modeled based on Document Object Model. class is appropriate if you need to extract data in a non-sequential manner. Figure 3 below outlines parsing of

 1 Imports System.IO 2 Imports System. 3 Module ParsingUsing 4 Sub Main() 5  Try 6   Dim m_As  7   Dim m_nodelist As  8   Dim m_node As  9   'Create the 10   m_New 11   'Load the 12   m_"C:\CMS\Personal\family.")13   'Get the list of name nodes 14   m_nodelist = m_"/family/name")15   'Loop through the nodes16   For Each m_node In m_nodelist17    'Get the Gender Attribute Value18    Dim genderAttribute = m_node.Attributes.GetNamedItem("gender").Value19    'Get the firstName Element Value20    Dim firstNameValue = m_node.ChildNodes.Item(0).InnerText21    'Get the lastName Element Value22    Dim lastNameValue = m_node.ChildNodes.Item(1).InnerText23    'Write Result to the Console24    Console.Write("Gender: " & genderAttribute _25     & " FirstName: " & firstNameValue & " LastName: " _26     & lastNameValue)27    Console.Write(vbCrLf)28   Next29  Catch errorVariable As Exception30   'Error trapping31   Console.Write(errorVariable.ToString())32  End Try33 End Sub34 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/


原标题:[vb.net]XML File Parsing in VB.NET

关键词:.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

如何拯救70%的购物车弃单率?10个策略帮你挽回客户!:https://www.kjdsnews.com/a/1379744.html
没想到Facebook擦边电商,黑五类产品还能这样玩?:https://www.kjdsnews.com/a/1379745.html
敦煌网加速海外布局,未来3年要在美国建设100个线下多功能空间:https://www.kjdsnews.com/a/1379746.html
实战经验:注册美国公司如何选择类型?:https://www.kjdsnews.com/a/1379747.html
申请注册美国公司的优势及好处:https://www.kjdsnews.com/a/1379748.html
注册美国公司流程知识普及:https://www.kjdsnews.com/a/1379749.html
九月初新疆旅游服装搭配(新疆游玩必备衣服清单):https://www.vstour.cn/a/408257.html
黄果树瀑布景区景点 - 黄果树瀑布景区景点分布图:https://www.vstour.cn/a/408258.html
我的浏览记录
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流