1 p.FromXmlFile("SampleFile.xml"); 2 3 Debug.WriteLine( 4 String.Format( 5 "{0} was born {1}, and lives at {2}, {3}", 6 p.Name, 7 p.DateOfBirth.ToString(), 8 p.Address.HouseNo, 9 p.Address.PostCode)); 10 11 Debug.WriteLine( 12 String.Format( 13 "Cars Owned ({0})", p.Cars.Count)); 14 foreach (Car c in p.Cars) 15 ...{ 16 Debug.WriteLine( 17 string.Format(" {0}, {1}", 18 c.Make, 19 c.Model)); 20 } | 1 XmlDocument xmlDoc = new XmlDocument(); 2 xmlDoc.Load("SampleFile.xml"); 3 4 XmlElement xmlElmPerson = GetFirstElement(xmlDoc); 5 if (xmlElmPerson == null || xmlElmPerson.Name != "Person") 6 throw new Exception("Must start with Person"); 7 8 XmlElement xmlElmName = GetFirstElement(xmlElmPerson); 9 if (xmlElmName == null || xmlElmName.Name != "Name") 10 throw new Exception("Missing Person->Name"); 11 12 XmlElement xmlElmDOB = GetNextElement(xmlElmName); 13 if (xmlElmDOB == null || xmlElmDOB.Name != "DateOfBirth") 14 throw new Exception("Missing Person->DateOfBirth"); 15 16 XmlElement xmlElmAddress = GetNextElement(xmlElmDOB); 17 if (xmlElmAddress == null || xmlElmAddress.Name != "Address") 18 throw new Exception("Missing Person->Address"); 19 20 21 XmlElement xmlElmHouseNo = GetFirstElement(xmlElmAddress); 22 if (xmlElmHouseNo == null || xmlElmHouseNo.Name != "HouseNo") 23 throw new Exception("Missing Person->Address->HouseNo"); 24 25 XmlElement xmlElmPostCode = GetNextElement(xmlElmHouseNo); 26 if (xmlElmPostCode == null || xmlElmPostCode.Name != "PostCode") 27 throw new Exception("Missing Person->Address->PostCode"); 28 29 if (GetNextElement(xmlElmPostCode) != null) 30 throw new Exception("Unexpected Element found"); 31 32 Debug.WriteLine( 33 String.Format( 34 "{0} was born {1}, and lives at {2}, {3}", 35 xmlElmName.InnerText, 36 xmlElmDOB.InnerText, 37 xmlElmHouseNo.InnerText, 38 xmlElmPostCode.InnerText)); 39 40 XmlElement xmlElmCar = GetNextElement(xmlElmAddress); 41 while (xmlElmCar != null) 42 ...{ 43 if (xmlElmCar.Name != "Car") 44 throw new Exception("Unknown element " + xmlElmCar.Name); 45 46 XmlElement xmlElmMake = GetFirstElement(xmlElmCar); 47 if (xmlElmMake == null || xmlElmMake.Name != "Make") 48 throw new Exception("Missing Person->Car->Make"); 49 50 XmlElement xmlElmModel = GetNextElement(xmlElmMake); 51 if (xmlElmModel == null || xmlElmModel.Name != "Model") 52 throw new Exception("Missing Person->Car->Model"); 53 54 if (GetNextElement(xmlElmModel) != null) 55 throw new Exception("Unexpected Element found"); 56 57 Debug.WriteLine( 58 string.Format(" {0}, {1}", 59 xmlElmMake.InnerText, 60 xmlElmModel.InnerText)); 61 62 xmlElmCar = GetNextElement(xmlElmCar); 63 } 1 private static XmlElement GetFirstElement(XmlNode xmlParent) 2 ...{ 3 if (xmlParent == null) 4 return null; 5 else if (xmlParent.FirstChild == null) 6 return null; 7 else if (xmlParent.FirstChild is XmlElement) 8 return xmlParent.FirstChild as XmlElement; 9 else 10 return GetNextElement(xmlParent.FirstChild); 11 } 12 13 private static XmlElement GetNextElement(XmlNode xmlNode) 14 ...{ 15 while (xmlNode != null) 16 ...{ 17 xmlNode = xmlNode.NextSibling; 18 if (xmlNode is XmlElement) 19 return xmlNode as XmlElement; 20 } 21 return null; 22 } |