나는 이것이 가능하지만 의심 스럽습니다. 요소의 태그 이름이 속성 값을 채우는 데 사용되는 방식으로 xml을 deserialize 할 수 있습니까?XML은 요소 태그를 기준으로 값을 역 직렬화합니다.
Class Animal
Property Name As String
Property Type As String
End Class
Name Type
------- -------
Jake Dog
Mittens Cat
나는 이것이 가능하지만 의심 스럽습니다. 요소의 태그 이름이 속성 값을 채우는 데 사용되는 방식으로 xml을 deserialize 할 수 있습니까?XML은 요소 태그를 기준으로 값을 역 직렬화합니다.
Class Animal
Property Name As String
Property Type As String
End Class
Name Type
------- -------
Jake Dog
Mittens Cat
그래서, XML을 시리얼로, 그러나, 다음의 XmlReader를 (XmlTextReader는) 그것을 해결할 수 없습니다 :
<Test>
<List>
<Jake Type="Dog" />
<Mittens Type="Cat" />
</List>
</Test>
이 같은 목록이 될 수 있습니다 : 예를 들어이 XML을 부여 방법 :이 사용한다면 잘
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