如何用xml属性解决序列化歧义?

时间:2013-04-23 10:23:10

标签: c#-4.0 xml-serialization xml-validation xml-attribute

我想用XmlSerializer序列化一个有效的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 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

显然,问题在于两个描述的模糊性。我试图弄清楚如何用xml属性解决它,但到目前为止还没有运气。

如果有人有想法,请随意分享=)

干杯!

1 个答案:

答案 0 :(得分:0)

我设法搞清楚了。问题出在DdiCategory类中,description属性的类型错误。该类型应为StructuredStringType而不是DdiContent。在此之后,我可以消除xml属性。