所以,不使用XML序列化,但是,你可以用以下的的XmlReader(XmlTextReader的)解決問題方法:
Class Animal
Public Property Name As String
Public Property Type As String
End Class
Function ReadDocument(filename As String) As List(Of Animal)
Dim lst As New List(Of Animal)
Dim doc As XmlReader
Using fs As FileStream = New FileStream(filename, FileMode.Open, FileAccess.Read)
doc = New Xml.XmlTextReader(fs)
While doc.Read()
If doc.NodeType <> XmlNodeType.Element Then
Continue While
End If
If Not String.Equals(doc.Name, "List") Then
Continue While
End If
While doc.Read()
If doc.NodeType = XmlNodeType.EndElement And String.Equals(doc.Name, "List") Then
Exit While
End If
If doc.NodeType <> XmlNodeType.Element Then
Continue While
End If
Dim ani As New Animal
ani.Name = doc.Name
If doc.MoveToAttribute("Type") Then
ani.Type = doc.Value
lst.Add(ani)
End If
End While
End While
End Using
Return lst
End Function
Sub Main()
Dim animals As List(Of Animal) = ReadDocument("./Animals.xml")
Console.WriteLine("{0}{1}{2}", "Name", vbTab, "Type")
For Each ani As Animal In animals
Console.WriteLine("{0}{1}{2}", ani.Name, vbTab, ani.Type)
Next
Console.ReadLine()
End Sub
好,不與XmlSerializer類,但是,你能做到這一點使用XmlDocumentReader – Icepickle 2014-10-30 19:04:11