This project is read-only.

Cannot set Properties using UnionSimpleTypeValidator

Dec 19, 2012 at 5:50 PM
Edited Dec 19, 2012 at 5:56 PM
Is there an issue with setting properties using the UnionSimpleTypeValidator? I have generated class from a xsd file and have two properties whose setters are similar
this.SetElementWithValidation(XName.Get("Product", "http://www.ros.ie/schemas/rom1/v1/"), value, "Product", global::www.ros.ie.schemas.rom1.v1.ControlledProductType.TypeDefinition);

and

this.SetElementWithValidation(XName.Get("Product", "http://www.ros.ie/schemas/rom1/v1/"), value, "Product", global::www.ros.ie.schemas.rom1.v1.AllProductType.TypeDefinition);
The value should be set to an int and the former setter works, but the latter gives an error 
 Xml type 'xdt:anyAtomicType' does not support a conversion from Clr type 'Int32' to Clr type 'String'.

The definitions are as follows.
 public sealed class AllProductType {
        
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        public static Xml.Schema.Linq.SimpleTypeValidator TypeDefinition = new Xml.Schema.Linq.UnionSimpleTypeValidator(XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.AnyAtomicType), null, new Xml.Schema.Linq.SimpleTypeValidator[] {
                    www.ros.ie.schemas.rom1.v1.ControlledProductType.TypeDefinition,
                    www.ros.ie.schemas.rom1.v1.NonControlledProductType.TypeDefinition});
        
        private AllProductType() {
        }
    }
    
    /// <summary>
    /// <para>
    /// 3 = DERV, 5 = ULTRA_LOW_SULPHUR, 6 = MARKED_GAS_OIL, 7=KEROSENE
    /// </para>
    /// </summary>
    public sealed class ControlledProductType {
        
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        public static Xml.Schema.Linq.SimpleTypeValidator TypeDefinition = new Xml.Schema.Linq.AtomicSimpleTypeValidator(XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.Int), new Xml.Schema.Linq.RestrictionFacets(((Xml.Schema.Linq.RestrictionFlags)(16)), new object[] {
                        3,
                        5,
                        6,
                        7}, 0, 0, null, null, 0, null, null, 0, null, 0, XmlSchemaWhiteSpace.Collapse));
        
        private ControlledProductType() {
        }
    }
    
    /// <summary>
    /// <para>
    /// 1 = PETROL, 2 = AVIATION_GASOLINE, 4 = HEAVY_AIR_NAV, 8 = SUB_FUEL_PRO_PETROL,
    ///			9 = SUB_FUEL_PRO_DIESEL, 10 = LPG_PROPELLANT, 11 = LPG_OTHER
    /// </para>
    /// </summary>
    public sealed class NonControlledProductType {
        
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        public static Xml.Schema.Linq.SimpleTypeValidator TypeDefinition = new Xml.Schema.Linq.AtomicSimpleTypeValidator(XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.Int), new Xml.Schema.Linq.RestrictionFacets(((Xml.Schema.Linq.RestrictionFlags)(16)), new object[] {
                        1,
                        2,
                        4,
                        8,
                        9,
                        10,
                        11}, 0, 0, null, null, 0, null, null, 0, null, 0, XmlSchemaWhiteSpace.Collapse));
        
        private NonControlledProductType() {
        }
    }

You can download the schemas from http://www.revenue.ie/en/customs/businesses/importing/aep/schemas/romreturn/v1/schema.xsd

In this particular case it is fairly easy to around the issue by redefining AllProductType not to use union, but list all the items explicitly.

Dec 28, 2016 at 6:30 PM
Edited Dec 28, 2016 at 6:30 PM
Hi,

I have the same problem, however changing XSD schema is not an option for me as I got it (as-is) from the vendor. I located the code that is making trouble for me.

xsd part:
<xsd:simpleType name="TNrREGON">
        <xsd:annotation>
            <xsd:documentation>Numer REGON</xsd:documentation>
        </xsd:annotation>
        <xsd:union>
            <xsd:simpleType>
                <xsd:restriction base="xsd:string">
                    <xsd:pattern value="\d{9}"/>
                </xsd:restriction>
            </xsd:simpleType>
            <xsd:simpleType>
                <xsd:restriction base="xsd:string">
                    <xsd:pattern value="\d{14}"/>
                </xsd:restriction>
            </xsd:simpleType>
        </xsd:union>
    </xsd:simpleType>
fix in XObjects.cs in XTypedServices class:
internal static string GetXmlString(object value, XmlSchemaDatatype datatype, XElement element) {
            string stringValue = null;
            if (datatype.TypeCode == XmlTypeCode.QName) {
                XmlQualifiedName qName = value as XmlQualifiedName;
                Debug.Assert(qName != null);
                stringValue = XTypedServices.QNameToString(qName, element);
            }
            /* fix start */
            else if (datatype.TypeCode == XmlTypeCode.AnyAtomicType)
            {
                stringValue = value as string;
            }
            /* fix end */
            else
                stringValue = (string)datatype.ChangeType(value, XTypedServices.typeOfString);

            return stringValue;
        }