我正在进行集成,我从外部API接收数据作为xml文件。我使用XSD.exe从关联的XSD文件生成C#类。我使用XmlDocument,将其序列化为类定义的相应对象,并可以作为类的属性访问所有元素。但是,该类将每个元素定义为Object类型,而不是XSD中给出的数据类型。这可以防止我访问数据或写入适当的新数据。为什么XSD.exe不使用定义的类型?是否有一些设置我没有注意到XSD.exe?有没有办法将Object中的数据转换为适当的数据类型?
示例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>
`...
示例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" />
自动生成的C#类(请注意Object数据类型)
...
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;
...