XSD.exe Generating Classes with type Object instead of XSD defined type

1.4k views Asked by At

I am working on an integration where I receive data as xml files from an external API. I used XSD.exe to generate C# classes from the associated XSD file. I take the XmlDocument, serialize it into the appropriate object defined by the class, and can access all elements as properties of the class. However the class defines each element as type Object instead of the data type given in the XSD. This prevents me from accessing the data or writing the appropriate new data. Why does XSD.exe not use the defined type? Is there some setting I haven't noticed for XSD.exe? Is there a way to cast the data found in Object into the appropriate data type?

Example Xml:

  <?xml version="1.0" encoding="UTF-8" ?> 
- <xmldata>
- <Orders>
  <OrderID>1</OrderID> 
  <AccountNumber /> 
  <AccountType /> 
  <AddressValidated>Y</AddressValidated> 
  <Affiliate_Commissionable_Value>45.0000</Affiliate_Commissionable_Value> 
  <AuthHash>a3d2f5c2599fc38e5f211ea52abc1040</AuthHash> 
  <BankName /> 
  <BillingAddress1>123 St.</BillingAddress1> 
  <BillingAddress2>United States of America</BillingAddress2> 
  <BillingCity>Metropolis</BillingCity> 
  <BillingCompanyName /> 
  <BillingCountry>United States</BillingCountry> 
  <BillingFaxNumber /> 
  <BillingFirstName>Firsty</BillingFirstName> 
  <BillingLastName>Lasty</BillingLastName> 
  <BillingPhoneNumber>1234567890</BillingPhoneNumber> 
  <BillingPostalCode>12345</BillingPostalCode> 
  <BillingState>US</BillingState> 
`...

Example XSD:

    <?xml version="1.0" encoding="utf-8"?>
<xs:schema id="xmldata" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop">
  <xs:element name="xmldata" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="Orders">
          <xs:complexType>
            <xs:sequence>
              <xs:element minOccurs="0" msprop:SqlDbType="Int" name="OrderID" msprop:TableNameXsd="Orders" msprop:IsIdentity="true" msprop:SQLTableAlias="o" />
              <xs:element name="AccountNumber" msprop:SqlDbType="VarChar" minOccurs="0" msprop:SQLTableAlias="o" msprop:maxLength="64" />
              <xs:element name="AccountType" msprop:SqlDbType="VarChar" minOccurs="0" msprop:SQLTableAlias="o" msprop:maxLength="10" />
              <xs:element name="AddressValidated" msprop:SqlDbType="VarChar" minOccurs="0" msprop:SQLTableAlias="o" msprop:maxLength="1" />
              <xs:element name="Affiliate_Commissionable_Value" msprop:SqlDbType="Money" minOccurs="0" msprop:SQLTableAlias="o" />
              <xs:element name="AuthHash" msprop:SqlDbType="VarChar" minOccurs="0" msprop:SQLTableAlias="o" msprop:maxLength="40" />
              <xs:element name="AVS" msprop:SqlDbType="VarChar" minOccurs="0" msprop:SQLTableAlias="o" msprop:maxLength="25" />
              <xs:element name="BankName" msprop:SqlDbType="VarChar" minOccurs="0" msprop:SQLTableAlias="o" msprop:maxLength="128" />
              <xs:element name="BatchNumber" msprop:SqlDbType="Int" minOccurs="0" msprop:SQLTableAlias="o" />
              <xs:element name="BillingAddress1" msprop:SqlDbType="VarChar" minOccurs="0" msprop:SQLTableAlias="o" msprop:maxLength="75" />
              <xs:element name="BillingAddress2" msprop:SqlDbType="VarChar" minOccurs="0" msprop:SQLTableAlias="o" msprop:maxLength="75" />
              <xs:element name="BillingCity" msprop:SqlDbType="VarChar" minOccurs="0" msprop:SQLTableAlias="o" msprop:maxLength="45" />
              <xs:element name="BillingCompanyName" msprop:SqlDbType="VarChar" minOccurs="0" msprop:SQLTableAlias="o" msprop:maxLength="100" />

Autogenerated C# Class(Note the Object datatype)

...
    public partial class xmldata
{

    private xmldataOrders[] itemsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Orders", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public xmldataOrders[] Items
    {
        get
        {
            return this.itemsField;
        }
        set
        {
            this.itemsField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class xmldataOrders
{

    private object orderIDField;

    private object accountNumberField;

    private object accountTypeField;

    private object addressValidatedField;

    private object affiliate_Commissionable_ValueField;

    private object authHashField;

    private object aVSField;

    private object bankNameField;

    private object batchNumberField;

    private object billingAddress1Field;

    private object billingAddress2Field;

    private object billingCityField;

    private object billingCompanyNameField;

    private object billingCountryField;

    private object billingFaxNumberField;

    private object billingFirstNameField;
...
0

There are 0 answers