The code needed to read the PersonSample.xml document using Xml Data Binding.
p.FromXmlFile("SampleFile.xml");
Debug.WriteLine(
String.Format(
"{0} was born {1}, and lives at {2}, {3}",
p.Name,
p.DateOfBirth.ToString(),
p.Address.HouseNo,
p.Address.PostCode));
Debug.WriteLine(
String.Format(
"Cars Owned ({0})", p.Cars.Count));
foreach (Car c in p.Cars)
{
Debug.WriteLine(
string.Format(" {0}, {1}",
c.Make,
c.Model));
}
The code needed to read the PersonSample.xml document using MSXML.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("SampleFile.xml");
XmlElement xmlElmPerson = GetFirstElement(xmlDoc);
if (xmlElmPerson == null || xmlElmPerson.Name != "Person")
throw new Exception("Must start with Person");
XmlElement xmlElmName = GetFirstElement(xmlElmPerson);
if (xmlElmName == null || xmlElmName.Name != "Name")
throw new Exception("Missing Person->Name");
XmlElement xmlElmDOB = GetNextElement(xmlElmName);
if (xmlElmDOB == null || xmlElmDOB.Name != "DateOfBirth")
throw new Exception("Missing Person->DateOfBirth");
XmlElement xmlElmAddress = GetNextElement(xmlElmDOB);
if (xmlElmAddress == null || xmlElmAddress.Name != "Address")
throw new Exception("Missing Person->Address");
XmlElement xmlElmHouseNo = GetFirstElement(xmlElmAddress);
if (xmlElmHouseNo == null || xmlElmHouseNo.Name != "HouseNo")
throw new Exception("Missing Person->Address->HouseNo");
XmlElement xmlElmPostCode = GetNextElement(xmlElmHouseNo);
if (xmlElmPostCode == null || xmlElmPostCode.Name != "PostCode")
throw new Exception("Missing Person->Address->PostCode");
if (GetNextElement(xmlElmPostCode) != null)
throw new Exception("Unexpected Element found");
Debug.WriteLine(
String.Format(
"{0} was born {1}, and lives at {2}, {3}",
xmlElmName.InnerText,
xmlElmDOB.InnerText,
xmlElmHouseNo.InnerText,
xmlElmPostCode.InnerText));
XmlElement xmlElmCar = GetNextElement(xmlElmAddress);
while (xmlElmCar != null)
{
if (xmlElmCar.Name != "Car")
throw new Exception("Unknown element " + xmlElmCar.Name);
XmlElement xmlElmMake = GetFirstElement(xmlElmCar);
if (xmlElmMake == null || xmlElmMake.Name != "Make")
throw new Exception("Missing Person->Car->Make");
XmlElement xmlElmModel = GetNextElement(xmlElmMake);
if (xmlElmModel == null || xmlElmModel.Name != "Model")
throw new Exception("Missing Person->Car->Model");
if (GetNextElement(xmlElmModel) != null)
throw new Exception("Unexpected Element found");
Debug.WriteLine(
string.Format(" {0}, {1}",
xmlElmMake.InnerText,
xmlElmModel.InnerText));
xmlElmCar = GetNextElement(xmlElmCar);
}
private static XmlElement GetFirstElement(XmlNode xmlParent)
{
if (xmlParent == null)
return null;
else if (xmlParent.FirstChild == null)
return null;
else if (xmlParent.FirstChild is XmlElement)
return xmlParent.FirstChild as XmlElement;
else
return GetNextElement(xmlParent.FirstChild);
}
private static XmlElement GetNextElement(XmlNode xmlNode)
{
while (xmlNode != null)
{
xmlNode = xmlNode.NextSibling;
if (xmlNode is XmlElement)
return xmlNode as XmlElement;
}
return null;
}
Because the XML Data Binding code does the bulk of the parsing for you, manipulating the XML objects is just a matter of dealing with collections of objects, no need to continually check the type of an item etc.