Как решить неоднозначность сериализации с атрибутами xml?

Я хочу сериализовать действительный xml с помощью XmlSerializer. Но я получил это сообщение об ошибке:

«Произошла ошибка, отражающая свойство «Описания». ---> System.InvalidOperationException: верхний XML-элемент «Описание» из пространства имен «ddi:reusable:3

[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>();
    }
}
dev» ссылается на разные типы System.Collections.Generic.List`1[Opit. Rogatus.DdiObjects.DdiContent] и Opit.Rogatus.DdiObjects.DdiContent. Используйте атрибуты XML, чтобы указать другое имя или пространство имен XML для элемента или типов.."

У меня есть этот класс, который нужно сериализовать. И есть свойство Descriptions, которое помечено для сериализатора, чтобы переименовать его в 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, но пока не повезло.

Если у кого-то есть идеи, поделитесь =)

Ваше здоровье!


person JahManCan    schedule 23.04.2013    source источник


Ответы (1)


Мне удалось разобраться. Проблема заключалась в том, что в классе DdiCategory свойство description имело неправильный тип. Этот тип должен быть StructuredStringType, а не DdiContent. После этого я мог удалить атрибуты xml.

person JahManCan    schedule 29.04.2013