我想用XmlSerializer序列化一個有效的xml。但是我得到了這個錯誤信息:如何用xml屬性解決序列化歧義?
「反映屬性'描述'時出現錯誤---> System.InvalidOperationException:命名空間'ddi:reusable:3_2_dev'的頂級XML元素'Description'引用不同類型System .Collections.Generic.List`1 [Opit.Rogatus.DdiObjects.DdiContent]和Opit.Rogatus.DdiObjects.DdiContent。使用XML屬性爲元素指定另一個XML名稱或名稱空間..「
我有這個類需要被序列化。還有一個屬性說明標記爲序列化程序將其重命名爲描述。但是由於在可重用的命名空間中顯然存在另一個Description,我得到了一個錯誤。
XSD的類別:
<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>();
}
}
可重複使用的命名空間:
很明顯的一個問題是兩個描述歧義。我試圖找出它如何解決它與XML屬性,但迄今沒有運氣。
如果有人有想法請隨時分享=)
乾杯!