views:

550

answers:

1

I'm trying to deserialize XML with two namespaces, like this

<records xmlns="http://www.foo.com/xml/records/1.1"&gt;
    <record xmlns="http://www.foo.com/xml/record/1.1"&gt;

and sometimes an older version

<records xmlns="http://www.foo.com/xml/records/1.1"&gt;
    <record xmlns="http://www.foo.com/xml/record/1.0"&gt;

My Records.cs class has

[XmlRoot(ElementName = "records", Namespace = "http://www.foo.com/xml/records/1.1")]
public class Records
{
    [System.Xml.Serialization.XmlElementAttribute("record")]
    public List<Record> Records { get; set; }
}

I want the Records list to be able to contain either a version 1.0 or version 1.1 Record

/// <remarks/>
[XmlRoot(IsNullable = false, ElementName = "record", Namespace = "http://www.foo.com/xml/record/1.0")]
public partial class Record
{


    /// <remarks/>

    public Record()
    {

    }
}

/// <remarks/>
[XmlRoot(IsNullable = false, ElementName = "record", Namespace = "http://www.foo.com/xml/record/1.1")]
public partial class Record11 : Record
{
    /// <remarks/>
    public Record11()
    {
    }
}

so I assumed subclassing the record would work.

I get a Reflection exception when deserializing and the exception points me to the XmlChoiceIdentifier attribute. However, that seems related to enums.

Anyone know how to do what I want to do (support deserializing multiple versions of the same schema?)

Thanks.

+2  A: 

[XmlRoot] attributes on both Record and Record11 in your example will be ignored. They only have meaning when element is a root in the tree. What you rather need to do is this:

[XmlRoot(ElementName = "records",
         Namespace = "http://www.foo.com/xml/records/1.1")]
public class Records
{
    [XmlElement(Type = typeof(Record),
                ElementName = "record",
                Namespace = "http://www.foo.com/xml/records/1.0")]
    [XmlElement(Type = typeof(Record11),
                ElementName = "record",
                Namespace = "http://www.foo.com/xml/records/1.1")]
    public List<Record> Records { get; set; }
}
Pavel Minaev
Aces! thanks a lot
jimbojones