All pastes #938804 Raw Edit

Miscellany

public text v1 · immutable
·published 2008-03-11 21:29 UTC
rendered paste body
Class Person
    Property Forename As String
    Property Surname As String

    Public Sub SaveToXml(doc As XmlDocument, node As XmlElement)
        Dim child As XmlElement

        child = doc.CreateElement("Forename")
        child.InnerText = Forename
        node.Append(child)

        child = doc.CreateElement("Surname")
        child.InnerText = Surname
        node.Append(child)
    End Sub

    Public Sub LoadFromXml(node As XmlElement)
        Dim child As XmlElement

        child = node.SelectSingleNode("Forename")
        If child IsNot Nothing Then Forename = child.InnerText Else Forename = ""

        child = node.SelectSingleNode("Surname")
        If child IsNot Nothing Then Surname = child.InnerText Else Surname = ""
    End Sub
End Class


To save you then need something like:

Dim people As New List(Of Person)
//Do some gui work here to let the user create people and edit their names

Dim doc As New XmlDocument()
Dim root As XmlElement = doc.CreateElement("People")

doc.DocumentElement = root

For Each p As Person In people
    Dim node as XmlElement = doc.CreateElement("Person")
    p.SaveToXml(doc, node)
    root.Append(node)
Next

doc.Save(filename)


To load:
Dim people As New List(Of Person)
Dim doc as New XmlDocument()
doc.Load(filename)

For Each item As XmlElement In doc.Select("//People")
    Dim p as New Person()
    p.LoadFromXml(item)
    people.Add(p)
Next