I'm trying to deserialize XML where some same name tags have different xsi types:
<user-defined-data-row>
<field name="entity">
<field-value xsi:type="field-text-valueType">
<value>Test</value>
</field-value>
</field>
<field name="expiry_date">
<field-value xsi:type="field-date-valueType">
<value>2001-10-07</value>
</field-value>
</field>
</user-defined-data-row>
This is easily achieved by deserializing the xml into this model:
[XmlRoot(ElementName = "field-value", Namespace = "http://www.crsoftwareinc.com/xml/ns/titanium/common/v1_0")]
[XmlType("field-text-valueType")]
public class Fieldvalue
{
[XmlElement(ElementName = "value", Namespace = "http://www.crsoftwareinc.com/xml/ns/titanium/common/v1_0")]
public string Value { get; set; }
}
The only thing that differs are the types in the XML:
field-text-valueType
field-date-valueType
How can I make the C# class interpret both types using something like
[XmlType("field-text-valueType")]
EDIT: deserializing not serializing
The
xsi:type
attributes you are seeing in your XML are standard W3C XML Schema attributes that allow an element to explicitly specify its type; for details see here. As explained in Xsi:type Attribute Binding Support,XmlSerializer
supports this mechanism for deserialization of polymorphic types, specifically by use ofXmlIncludeAttribute
.First, define an abstract base class
FieldValue
as follows:Next, for each possible
xsi:type="XXX"
value, define a derived type ofFieldValue
whoseXmlTypeAttribute.TypeName
matches thexsi:type
value. Decorate the base class with[XmlInclude(typeof(TDerivedFieldValue))]
attributes for each (already shown above):Then define the containing type corresponding to
<field>
and other, higher elements as follows:Notes:
If the base class
FieldValue
has a namespace specified in viaXmlTypeAttribute.Namespace
, then the derived classes must also, or else an error will get thrown byXmlSerializer
.Once an
[XmlType]
namespace is defined, it automatically applies to all serialized properties, so it isn't necessary to specify the same namespace via[XmlElement(Namespace = "http://www.crsoftwareinc.com/xml/ns/titanium/common/v1_0")]
attributes.I got tired of repeatedly typing the namespace
"http://www.crsoftwareinc.com/xml/ns/titanium/common/v1_0"
and so I extracted it into a constant.Other derived types of
FieldType
can be added easily, e.g.:Don't forget to add
[XmlInclude(typeof(DecimalFieldValue))]
when doing so.If you have been given an XSD for the XML you are trying to deserialize that specifies the possible types of
<field-value>
via e.g. an<xsd:extension>
element as shown in Generating XML Documents from XML Schemas: Abstract Types, thenxsd.exe
will generate classes that include an appropriate type hierarchy. But if you only have the XML, thenxsd.exe
and Paste XML as Classes will not generate a correct type hierarchy using whateverxsi:type
attributes are present.For more about this limitation see xsi:type attribute messing up C# XML deserialization.
Your XML is not well-formed because it omits a declaration for the
xsi:
namespace. Also, a default namespacexmlns="http://www.crsoftwareinc.com/xml/ns/titanium/common/v1_0"
is not defined so none of the elements are actually in this namespace. Thus I assume your XML is a fragment of some larger document that is valid, e.g.Sample working .Net fiddle here.