如何使用 .NET,從檔案中讀取 XML
原始的 Books.xml文件如下:
02 -
04 - <bookstore>
05 - <book genre="autobiography">
06 <title>The Autobiography of Benjamin Franklintitle>
07 - <author>
08 <first-name>Benjaminfirst-name>
09 <last-name>Franklinlast-name>
10 author>
11 <price>8.99price>
12 book>
13 - <book genre="novel">
14 <title>The Confidence Mantitle>
15 - <author>
16 <first-name>Hermanfirst-name>
17 <last-name>Melvillelast-name>
18 author>
19 <price>11.99price>
20 book>
21 - <book genre="philosophy">
22 <title>The Gorgiastitle>
23 - <author>
24 <name>Platoname>
25 author>
26 <price>9.99price>
27 book>
28 bookstore>
資料來源: http://support.microsoft.com/kb/301225/zh-tw
完整的程式碼清單
Imports System.Xml
Module Module1
Sub Main()
Dim reader As XmlTextReader = New XmlTextReader("books.xml")
Do While (reader.Read())
Select Case reader.NodeType
Case XmlNodeType.Element '註解:Display beginning of element.
Console.Write("<" + reader.Name)
If reader.HasAttributes Then '註解:If attributes exist
While reader.MoveToNextAttribute()
'Display attribute name and value.
Console.Write(" {0}='{1}'", reader.Name, reader.Value)
End While
End If
Console.WriteLine(">")
Case XmlNodeType.Text '註解:Display the text in each element.
Console.WriteLine(reader.Value)
Case XmlNodeType.EndElement '註解:Display end of element.
Console.Write("" + reader.Name)
Console.WriteLine(">")
End Select
Loop
Console.ReadLine()
End Sub
End Module
另外一個範例,也是透過 XMLReader.Read()方法來作
http://msdn.microsoft.com/zh-tw/library/system.xml.xmltextreader.read(VS.80).aspx
下列範例讀取 XML 檔案 並顯示每一個節點。
Option Explicit
Imports System
Imports System.IO
Imports System.Xml
'Reads an XML document
Public Class Sample
Private Const filename As String = "items.xml"
Public Shared Sub Main()
Dim reader As XmlTextReader = Nothing
Try
' Load the reader with the data file and ignore all white space nodes.
reader = New XmlTextReader(filename)
reader.WhitespaceHandling = WhitespaceHandling.None
' Parse the file and display each of the nodes.
While reader.Read()
Select Case reader.NodeType
Case XmlNodeType.Element
Console.Write("<{0}>", reader.Name)
Case XmlNodeType.Text
Console.Write(reader.Value)
Case XmlNodeType.CDATA
Console.Write("", reader.Value)
Case XmlNodeType.ProcessingInstruction
Console.Write("", reader.Name, reader.Value)
Case XmlNodeType.Comment
Console.Write("", reader.Value)
Case XmlNodeType.XmlDeclaration
Console.Write("'1.0'?>")
Case XmlNodeType.Document
Case XmlNodeType.DocumentType
Console.Write(", reader.Name, reader.Value)
Case XmlNodeType.EntityReference
Console.Write(reader.Name)
Case XmlNodeType.EndElement
Console.Write("{0}>", reader.Name)
End Select
End While
Finally
If Not (reader Is Nothing) Then
reader.Close()
End If
End Try
End Sub
End Class
類別 | 說明 |
---|---|
| 讀取字元資料流。它是順向讀取器,含有傳回內容和節點型別中之資料的方法。文件類型定義 (DTD) 或結構描述不受支援。若 XML 文件含有 DTD,則 DTD 將會被剖析,但不會用來進行驗證或加入預設屬性。http://msdn.microsoft.com/zh-tw/library/system.xml.xmltextreader.read(VS.80).aspx
|
透過 XML 文件物件模型 (DOM) API 提供 XmlReader,就像 XmlNode 樹狀結構。在 XmlNode 中取得,並傳回它在 DOM 樹狀結構中發現的任何節點,包括實體參照節點。它不支援 DTD 或結構描述驗證,但是它可以解析定義於 DTD 中的實體。http://msdn.microsoft.com/zh-tw/library/system.xml.xmlnodereader.read(VS.80).aspx |
沒有留言:
張貼留言