Imports System.Xml 读出的 xml 为什么有点不对?
Imports System.Xml
Imports System.IO
Module Module1
Sub Main()
Dim Stream As StreamWriter = File.AppendText( "C:\res.txt ")
Dim xr As XmlTextReader = New XmlTextReader(Directory.GetCurrentDirectory() & "\MyFile.xml ")
While xr.Read()
Select Case (xr.NodeType)
Case XmlNodeType.Comment
Stream.WriteLine( "Comment: " & xr.Value)
Case XmlNodeType.Element
Stream.WriteLine( "Element: " & xr.Value)
If (xr.HasAttributes) Then
Stream.WriteLine( "Attribute Count: " & xr.AttributeCount)
Dim intx As Integer
For intx = 0 To xr.AttributeCount - 1
Stream.WriteLine(xr.GetAttribute(intx))
Next
End If
Case XmlNodeType.Text
Stream.WriteLine( "Text: " & xr.Value)
Case XmlNodeType.Whitespace
Stream.WriteLine( "Whitespace ")
End Select
End While
Stream.Close()
End Sub
End Module
=======================================================
<?xml version= "1.0 " encoding= "utf-8 " ?>
<name>
<fname> James </fname>
<mname> Tiberius </mname>
<lname> Kirk </lname>
<specs position= "Captain " skip= "Enterprise " />
</name>
============================================================
结果是:
Whitespace
Element:
Whitespace
Element:
Text:James
Whitespace
Element:
Text:Tiberius
Whitespace
Element:
Text:Kirk
Whitespace
Element:
Attribute Count:2
Captain
Enterprise
Whitespace
Whitespace
=====================================================
为什么
<?xml version= "1.0 " encoding= "utf-8 " ?> 没读到?
Element: 后为什么没有东西?
[解决办法]
用Read读取节点时时从根节点 <name> 开始读吧,所以读不到 <?xml version= "1.0 " encoding= "utf-8 " ?>