1 Dim p As New Person() 2 3 ' Load the XML 4 p.FromXmlFile("SampleFile.xml") 5 6 Debug.WriteLine(String.Format( _ 7 "{0} was born {1}, " & _ 8 "and lives at {2}, {3}", _ 9 p.Name, _ 10 p.DateOfBirth.ToString(), _ 11 p.Address.HouseNo, _ 12 p.Address.PostCode)) 13 14 Debug.WriteLine(String.Format( _ 15 "Cars Owned ({0})", p.Cars.Count)) 16 For Each c As Car In p.Cars 17 Debug.WriteLine(String.Format( _ 18 " {0}, {1}", c.Make, c.Model)) 19 Next | 1 Dim xmlDoc As New XmlDocument() 2 xmlDoc.Load("SampleFile.xml") 3 4 Dim xmlElmPerson As XmlElement = GetFirstElement(xmlDoc) 5 If xmlElmPerson Is Nothing OrElse xmlElmPerson.Name <> "Person" Then 6 Throw New Exception("Must start with Person") 7 End If 8 9 Dim xmlElmName As XmlElement = GetFirstElement(xmlElmPerson) 10 If xmlElmName Is Nothing OrElse xmlElmName.Name <> "Name" Then 11 Throw New Exception("Missing Person->Name") 12 End If 13 14 Dim xmlElmDOB As XmlElement = GetNextElement(xmlElmName) 15 If xmlElmDOB Is Nothing OrElse xmlElmDOB.Name <> "DateOfBirth" Then 16 Throw New Exception("Missing Person->DateOfBirth") 17 End If 18 19 Dim xmlElmAddress As XmlElement = GetNextElement(xmlElmDOB) 20 If xmlElmAddress Is Nothing OrElse xmlElmAddress.Name <> "Address" Then 21 Throw New Exception("Missing Person->Address") 22 End If 23 24 25 Dim xmlElmHouseNo As XmlElement = GetFirstElement(xmlElmAddress) 26 If xmlElmHouseNo Is Nothing OrElse xmlElmHouseNo.Name <> "HouseNo" Then 27 Throw New Exception("Missing Person->Address->HouseNo") 28 End If 29 30 Dim xmlElmPostCode As XmlElement = GetNextElement(xmlElmHouseNo) 31 If xmlElmPostCode Is Nothing OrElse xmlElmPostCode.Name <> "PostCode" Then 32 Throw New Exception("Missing Person->Address->PostCode") 33 End If 34 35 If GetNextElement(xmlElmPostCode) IsNot Nothing Then 36 Throw New Exception("Unexpected Element found") 37 End If 38 39 Debug.WriteLine(String.Format( _ 40 "{0} was born {1}, and lives at {2}, {3}", _ 41 xmlElmName.InnerText, _ 42 xmlElmDOB.InnerText, _ 43 xmlElmHouseNo.InnerText, _ 44 xmlElmPostCode.InnerText)) 45 46 Dim xmlElmCar As XmlElement = GetNextElement(xmlElmAddress) 47 While xmlElmCar IsNot Nothing 48 If xmlElmCar.Name <> "Car" Then 49 Throw New Exception("Unknown element " & xmlElmCar.Name) 50 End If 51 52 Dim xmlElmMake As XmlElement = GetFirstElement(xmlElmCar) 53 If xmlElmMake Is Nothing OrElse xmlElmMake.Name <> "Make" Then 54 Throw New Exception("Missing Person->Car->Make") 55 End If 56 57 Dim xmlElmModel As XmlElement = GetNextElement(xmlElmMake) 58 If xmlElmModel Is Nothing OrElse xmlElmModel.Name <> "Model" Then 59 Throw New Exception("Missing Person->Car->Model") 60 End If 61 62 If GetNextElement(xmlElmModel) IsNot Nothing Then 63 Throw New Exception("Unexpected Element found") 64 End If 65 66 Debug.WriteLine(String.Format(" {0}, {1}", _ 67 xmlElmMake.InnerText, xmlElmModel.InnerText)) 68 69 xmlElmCar = GetNextElement(xmlElmCar) 70 End While 1 Private Function GetFirstElement()Function GetFirstElement(ByVal xmlParent As XmlNode) As XmlElement 2 If xmlParent Is Nothing Then 3 Return Nothing 4 ElseIf xmlParent.FirstChild Is Nothing Then 5 Return Nothing 6 ElseIf TypeOf xmlParent.FirstChild Is XmlElement Then 7 Return TryCast(xmlParent.FirstChild, XmlElement) 8 Else 9 Return GetNextElement(xmlParent.FirstChild) 10 End If 11 End Function 12 13 Private Function GetNextElement()Function GetNextElement(ByVal xmlNode As XmlNode) As XmlElement 14 While xmlNode IsNot Nothing 15 xmlNode = xmlNode.NextSibling 16 If TypeOf xmlNode Is XmlElement Then 17 Return TryCast(xmlNode, XmlElement) 18 End If 19 End While 20 Return Nothing 21 End Function |