I managed to figure it out. The problem was in DdiCategory class the description property had a wrong type. That type should be StructuredStringType and not DdiContent. After this I could eliminate the xml attributes.
How to solve serialization ambiguity with xml attributes?
-
11-04-2022 - |
Вопрос
I want to serialize a valid xml with an XmlSerializer. But I got this error message:
"There was an error reflecting property 'Descriptions'. ---> System.InvalidOperationException: The top XML element 'Description' from namespace 'ddi:reusable:3_2_dev' references distinct types System.Collections.Generic.List`1[Opit.Rogatus.DdiObjects.DdiContent] and Opit.Rogatus.DdiObjects.DdiContent. Use XML attributes to specify another XML name or namespace for the element or types.."
I have this class that needs to be serialized. And there is a property Descriptions which marked for the serializer to rename it to Description. But since there is another Description apparently in the reusable namespace I got an error.
xsd for category:
<xs:complexType name="CategoryType">
<xs:annotation>
<xs:documentation>A description of a particular category or response. OECD Glossary of Statistical Terms: Generic term for items at any level within a classification, typically tabulation categories, sections, subsections, divisions, subdivisions, groups, subgroups, classes and subclasses.</xs:documentation>
</xs:annotation>
<xs:complexContent>
<xs:extension base="r:VersionableType">
<xs:sequence>
<xs:element ref="CategoryName" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="r:Label" minOccurs="0" maxOccurs="unbounded">
<xs:annotation>
<xs:documentation>A display label for the category.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element ref="r:Description" minOccurs="0">
<xs:annotation>
<xs:documentation>Description/definition of the category. Note that comparison of categories is determined by the Definition rather than the Label. For example, while the Definition of a Chemist in London and a Pharmacist in New York is the same and comparable, the definitions of Chemist in each location differ significantly and are NOT comparable</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element ref="r:ConceptReference" minOccurs="0">
<xs:annotation>
<xs:documentation>Reference to a defining concept.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element ref="Generation" minOccurs="0">
<xs:annotation>
<xs:documentation>Generation/derivation details of the category.</xs:documentation>
</xs:annotation>
</xs:element>
</xs:sequence>
<xs:attribute name="missing" type="xs:boolean" use="optional">
<xs:annotation>
<xs:documentation>Indicates if the category contains missing data or not.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:extension>
</xs:complexContent>
[XmlRoot(ElementName = "Category", Namespace = LogicalProductNamespace)]
public class DdiCategory : AbstractVersionable
{
[XmlElement(ElementName = "CategoryName")]
public List<DdiName> CategoryNames { get; set; }
[XmlArray(ElementName = "Description", Namespace = ReusableNamespace)]
[XmlArrayItem(ElementName = "Content", IsNullable = false)]
public List<DdiContent> Descriptions { get; set; }
[XmlAttribute(AttributeName = "missing")]
public bool Missing { get; set; }
public DdiCategory()
{
Type = DomainObjectType.Category;
Descriptions = new List<DdiContent>();
}
}
Reusable namespace:
Obviously a problem is the ambiguity with the two descriptions. I tried to figure it out how could I solve it with xml attributes but so far no luck.
If anybody have an idea please feel free to share =)
Cheers!
Решение