具有多个命名空间的 Xml 选择标识符

XmlChoiceIdentifier with mulitple namespaces

我正在使用出色的 NIEM XML 模式。我试图反序列化的元素在其他名称空间中有替换和扩展。我不知道如何在 C# 中描述它。我正在尝试反序列化一条 WCF 消息,该消息包含来自 urn:oasis:names:tc:legalxml-courtfiling:schema:xsd:CommonTypes-4.0 名称空间中的 class 项 属性 中的 EntityPerson 元素].

Class定义

[System.Xml.Serialization.XmlIncludeAttribute(typeof(ServiceRecipientType1))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(ServiceRecipientType))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(CaseParticipantType1))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(CaseAbstractorType))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.8.3752.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://niem.gov/niem/niem-core/2.0")]
public partial class EntityType : ComplexObjectType {
    
    private object itemField;
    
    private ItemChoiceType2 itemElementNameField;
    
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("EntityOrganization", typeof(OrganizationType), Namespace = "urn:oasis:names:tc:legalxml-courtfiling:schema:xsd:CommonTypes-4.0", IsNullable =true, Order=0)]
    [System.Xml.Serialization.XmlElementAttribute("EntityOrganizationReference", typeof(ReferenceType), Namespace = "http://niem.gov/niem/niem-core/2.0", Order =0)]
    [System.Xml.Serialization.XmlElementAttribute("EntityPerson", typeof(PersonType), Namespace = "http://niem.gov/niem/niem-core/2.0", IsNullable=true, Order=0)]
    [System.Xml.Serialization.XmlElementAttribute("EntityPerson", typeof(PersonType1), Namespace = "urn:oasis:names:tc:legalxml-courtfiling:schema:xsd:CommonTypes-4.0", IsNullable = true, Order = 0)]
    [System.Xml.Serialization.XmlElementAttribute("EntityPersonReference", typeof(ReferenceType), Namespace = "http://niem.gov/niem/niem-core/2.0",  Order =0)]
    [System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemElementName")]
    public object Item {
        get {
            return this.itemField;
        }
        set {
            this.itemField = value;
            this.RaisePropertyChanged("Item");
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=1)]
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public ItemChoiceType2 ItemElementName {
        get {
            return this.itemElementNameField;
        }
        set {
            this.itemElementNameField = value;
            this.RaisePropertyChanged("ItemElementName");
        }
    }
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.8.3752.0")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(IncludeInSchema=false)]
public enum ItemChoiceType2 {
    
    /// <remarks/>
    EntityOrganization,
    
    /// <remarks/>
    EntityOrganizationReference,
    
    /// <remarks/>
    EntityPerson,
    
    /// <remarks/>
    EntityPersonReference,
}

XmlElementAttributes 中命名空间参数的任何组合只会导致如下所示的异常。如果我删除 Namespace 参数,那么 Item 属性 在反序列化时为 null。

System.InvalidOperationException: 'There was an error reflecting type 'MetadataType'.' InvalidOperationException: Type ItemChoiceType2 is missing enumeration value 'urn:oasis:names:tc:legalxml-courtfiling:schema:xsd:CommonTypes-4.0:EntityOrganization' for element 'EntityOrganization' from namespace 'urn:oasis:names:tc:legalxml-courtfiling:schema:xsd:CommonTypes-4.0'.

我需要它来使用我在 XmlElementAttributes 中定义的名称空间,但似乎当反序列化程序试图在 ItemChoiceType2 枚举中查找元素名称时,它也希望匹配名称空间。这不起作用,因为元素位于多个命名空间中。

我阅读了 https://it.ojp.gov/NISS/Downloads/KB/28 上的文档,但我似乎遇到了该文档中未列出的问题。 (不同命名空间需要 ItemChoice)

我已经尝试了上百万种命名空间参数排列,但就是找不到一个可行的方法。我如何定义这个 属性 以便它可以反序列化来自多个命名空间的元素?

好的,事实证明您可以为选择类型枚举赋予元素的完全限定名称。这解决了命名空间错误。

    /// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.8.3752.0")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(IncludeInSchema=false)]
public enum ItemChoiceType2 {
    
    /// <remarks/>
    [System.Xml.Serialization.XmlEnum("urn:oasis:names:tc:legalxml-courtfiling:schema:xsd:CommonTypes-4.0:EntityOrganization")]
    EntityOrganization,
    
    /// <remarks/>
    EntityOrganizationReference,

    [System.Xml.Serialization.XmlEnum("http://niem.gov/niem/niem-core/2.0:EntityPerson")]
    EntityPerson,

    /// <remarks/>
    [System.Xml.Serialization.XmlEnum("urn:oasis:names:tc:legalxml-courtfiling:schema:xsd:CommonTypes-4.0:EntityPerson")]
    EntityPerson1,
    
    /// <remarks/>
    EntityPersonReference,
}