6

I have given XML that I can't change and I need to deserialize it to a custom class:

<Person>
   <Addresses>
       <MainAddress>
          <Country />
          <City />
       </MainAddress>
       <AdditionalAddress>
          <Country />
          <City />
       </AdditionalAddress>
       <AdditionalAddress>
          <Country />
          <City />
       </AdditionalAddress>
       ... other additional addresses
   </Addresses>
   ... other elements
</Person>

Then I deserialize:

XmlSerializer serializer = new XmlSerializer(typeof(Person), namespace);
Person person = serializer.Deserialize(stream) as Person;       

Deserializer works fine in case simple elements are deserialized. When there is element like Addresses I implement IXmlSerializable on its class like here: link text

The problem is that reader.ReadElementContentAsString() cannot be used with complex elements.

2 Answers 2

4

Adressess class should be declared this way:

[XmlRoot("Adressess")]
public class Adressess
{
    [XmlElement(ElementName = "MainAddress")]
    public MainAddress Main { get; set; }

    [XmlElement(ElementName = "AdditionalAddress")]
    public List<AdditionalAddress> AdditionalAddresses { get; set; }
}

[XmlRoot("MainAddress")]
public class MainAddress 
{
    public string Country { get; set; }
    public string City { get; set; }
}

[XmlRoot("AdditionalAddress")]
public class AdditionalAddress
{
    public string Country { get; set; }
    public string City { get; set; }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Note that the type-name isn't relevant, so e.g. the MainAddress property could be implemented in a class MainAddressStore or whatever (I find it's sometimes confusing to have properties and types share names). This isn't very important for skeleton classes like this whose only purpose is serialization support, of course.
I've just edited the property names to be different that the class names. I'm just not sure if the distinct MainAddress and AdditionalAddress classes are really required. I wanted them to be annonated with different XmlRoot name.
I does not work. MainAddress is populated but AdditionalAddresses are not.
Have you use XmlElement no annotate properties in Addresses class? I corrected it a moment ago.
This is a rather bad approach. One common Address class would be enough, no need to have separate classes for main and additional addresses. I've just seen someone deriving from this example and being needlesly confused.
0

Also if the root object type has a namespace, you must use the same namespace for the nested types.

Example:

[System.Xml.Serialization.XmlRootAttribute(Namespace = "some namespace")]
class Person {...}

[System.Xml.Serialization.XmlRootAttribute(Namespace = "some namespace")]
class MainAddress{...}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.