Xml schema serialization of complextype with data annotation validations
I have the following schema part:
<xs:element name="authorizationNumber" type="authorizationIdType">
</xs:element>
<xs:complexType name="authorizationIdType">
<xs:choice>
<xs:element name="authorizationIdNumber" type="authorizationNumberType">
</xs:element>
<xs:element name="authorizationIdFree" type="xs:string">
</xs:element>
</xs:choice>
</xs:complexType>
<xs:simpleType name="authorizationNumberType">
<xs:restriction base="xs:string">
<xs:length value="17"/>
<xs:pattern value="(0[1-9]|1[0-9])(P0[1-3]|G0[1-5]|T0[1-2])d{12}"/>
</xs:restriction>
</xs:simpleType>
I generated the class.cs from the schema including data annotations (with xsd2code++) so i can validate the object. The corresponding part of the class.cs is like this:
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public authorizationIdType authorizationNumber { get; set; }
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="x")]
public partial class authorizationIdType
{
[System.Xml.Serialization.XmlElementAttribute("authorizationIdFree", typeof(string), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("authorizationIdNumber", typeof(string), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemElementName")]
public object Item { get; set; }
[System.Xml.Serialization.XmlIgnoreAttribute()]
public ItemChoiceType2 ItemElementName { get; set; }
}
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="x", IncludeInSchema=false)]
public enum ItemChoiceType2
{
[System.Xml.Serialization.XmlEnumAttribute(":authorizationIdFree")]
authorizationIdFree,
[System.Xml.Serialization.XmlEnumAttribute(":authorizationIdNumber")]
authorizationIdNumber
}
The problem is that as its a complex type the data annotations doesnt get included, but i dont know in this case how to include them as the have to be applied only when the public object Item is of the type authorizationIdNumber.
In a simple case of another element for example the annotations get generated like this:
[System.ComponentModel.DataAnnotations.StringLengthAttribute(5)]
[System.ComponentModel.DataAnnotations.RegularExpressionAttribute("[0-9]{5}")]
public string PostalCode{ get; set; }
Maybe the line:
[System.Xml.Serialization.XmlElementAttribute("authorizationIdNumber", typeof(string), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
Should be something like:
[System.Xml.Serialization.XmlElementAttribute("authorizationIdNumber", typeof(authorizationNumberType), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
And create a class type that holds the string value with the data annotations? This is the part that i dont know exactly how to code so the serialization and attribute instantiation continues working fine but with the Data Annotations validation included.
链接地址: http://www.djcxy.com/p/56672.html