国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[vb.net]XMLFileParsinginVB.NET

2019-11-14 15:52:29
字體:
來源:轉載
供稿:網友

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.NONE10   'read the xml declaration and advance to family tag11   m_xmlr.Read()12   'read the family tag13   m_xmlr.Read()14   'Load the Loop15   While Not m_xmlr.EOF16     'Go to the name tag17     m_xmlr.Read()18     'if not start element exit while loop19     If Not m_xmlr.IsStartElement() Then20       Exit While21     End If22     'Get the Gender Attribute Value23     Dim genderAttribute = m_xmlr.GetAttribute("gender")24     'Read elements firstname and lastname25     m_xmlr.Read()26     'Get the firstName Element Value27     Dim firstNameValue = m_xmlr.ReadElementString("firstname")28     'Get the lastName Element Value29     Dim lastNameValue = m_xmlr.ReadElementString("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_xmlr.Close()38 End Sub39 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 Document10     m_xmld = New XmlDocument()11     'Load the Xml file12     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 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/XML-File-Parsing-in-VB-NET


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 天门市| 扬中市| 龙陵县| 措勤县| 清涧县| 凉城县| 台北县| 星子县| 双鸭山市| 宿迁市| 安塞县| 高雄县| 沧源| 垦利县| 翁牛特旗| 白水县| 随州市| 建阳市| 达日县| 平乡县| 甘德县| 麟游县| 青田县| 赤城县| 高陵县| 台东市| 得荣县| 安庆市| 仙桃市| 河东区| 兴安盟| 卢龙县| 抚远县| 乌恰县| 吕梁市| 灯塔市| 休宁县| 车致| 米泉市| 水富县| 晋江市|