Spishixetters
New Member
I am trying to serialize/deserialize the following XML code structure:\[code\]<Person> <Name/> <Age/> <Address> <BuildingNumber/> <Street/> <Town/> <PostCode/> </Address></Person>\[/code\]using the classes\[code\]public class Person { public string Name { get; set; } public int Age { get; set; } public Address Address { get; set; }}public class Address { public string BuildingNumber { get; set; } public string Street { get; set; } public string Town { get; set; } public string PostCode { get; set; }}\[/code\]I am implemeting \[code\]IXmlSerializable\[/code\] on both classes (it's over-engineered for this specific example, I know, but it is required for a project I am working on which is rather more complicated!), for which serialisation works fine, but deserialisation does not.The problem arises in the \[code\]Person.ReadXml()\[/code\] method, which I have written as:\[code\]public void ReadXml(XmlReader reader) { reader.ReadStartElement(); if (!reader.IsEmptyElement) { Name = reader.ReadElementContentAsString("Name", string.Empty); Age = reader.ReadElementContentAsInt("Age", string.Empty); Address = (Address)reader.ReadElementContentAs(typeof(Address), null, "Address", string.Empty); // Also failed: Address = (Address)reader.ReadElementContentAsObject("Address", string.Empty); } reader.ReadEndElement();}\[/code\]The final line where \[code\]Address\[/code\] is being initialised is throwing an exception:\[quote\] ReadElementContentAs() methods cannot be called on an element that has child elements. (XmlException)\[/quote\]Basically, how would I handle elements with child elements using \[code\]IXmlSerializable\[/code\]?