2016-05-03 18 views
0

Ich habe eine Reihe von XSD-Dateien - sie sind riesig - für eine Versicherung Pricing Engine gegeben, so wie Sie sich vorstellen können, enthalten viele verschachtelte komplexe Typen . Person, Adresse, Fahrzeuge, Auto Alarm, Speeding Verurteilungen etc etc. Hier ist eine Teilmenge einen einzelnen komplexen Typen enthält:Wie stark typisiertes .NET Request Objekt von XSD in .NET generiert werden

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> 

    <xs:complexType name="Vehicle"> 
     <xs:choice maxOccurs="unbounded"> 
      <xs:element name="ABICode" type="ABICode" minOccurs="0"/> 
      <xs:element name="ABSBrakes" type="ABSBrakes" minOccurs="0"/> 
      <xs:element name="AgedOver" type="AgedOver" minOccurs="0"/> 
      <xs:element name="AlarmSecurityFitted" type="AlarmSecurityFitted" minOccurs="0"/> 
      <xs:element name="AlarmSecurityMake" type="AlarmSecurityMake" minOccurs="0"/> 
      <xs:element name="AudioValue" type="AudioValue" minOccurs="0"/> 
      <xs:element name="CarPhoneValue" type="CarPhoneValue" minOccurs="0"/> 
      <xs:element name="Colour" type="Colour" minOccurs="0"/> 
      <xs:element name="CountryOfManufacture" type="CountryOfManufacture" minOccurs="0"/> 
      <xs:element name="CoverType" type="CoverType" minOccurs="0"/> 
      <xs:element name="CurrentMileage" type="CurrentMileage" minOccurs="0"/> 
      <xs:element name="Doors" type="Doors" minOccurs="0"/> 
      <xs:element name="EngineSize" type="EngineSize" minOccurs="0"/> 
      <xs:element name="EstimatedValue" type="EstimatedValue" minOccurs="0"/> 
      <xs:element name="Finish" type="Finish" minOccurs="0"/> 
      <xs:element name="FuelType" type="FuelType" minOccurs="0"/> 
      <xs:element name="HasSecurity" type="HasSecurity" minOccurs="0"/> 
      <xs:element name="ImmobSecurityFitted" type="ImmobSecurityFitted" minOccurs="0"/> 
      <xs:element name="ImmobSecurityMake" type="ImmobSecurityMake" minOccurs="0"/> 
      <xs:element name="Keeper" type="Keeper" minOccurs="0"/> 
      <xs:element name="Lhd" type="Lhd" minOccurs="0"/> 
      <xs:element name="Modified" type="Modified" minOccurs="0"/> 
      <xs:element name="NCBCountry" type="NCBCountry" minOccurs="0"/> 
      <xs:element name="NCBProtected" type="NCBProtected" minOccurs="0"/> 
      <xs:element name="NCBType" type="NCBType" minOccurs="0"/> 
      <xs:element name="NCBYears" type="NCBYears" minOccurs="0"/> 
      <xs:element name="NightLocation" type="NightLocation" minOccurs="0"/> 
      <xs:element name="Owner" type="Owner" minOccurs="0"/> 
      <xs:element name="OvernightPostCode" type="OvernightPostCode" minOccurs="0"/> 
      <xs:element name="PermittedDrivers" type="PermittedDrivers" minOccurs="0"/> 
      <xs:element name="PricePaid" type="PricePaid" minOccurs="0"/> 
      <xs:element name="PurchaseDate" type="PurchaseDate" minOccurs="0"/> 
      <xs:element name="Registration" type="Registration" minOccurs="0"/> 
      <xs:element name="Seats" type="Seats" minOccurs="0"/> 
      <xs:element name="TrackerSecurityFitted" type="TrackerSecurityFitted" minOccurs="0"/> 
      <xs:element name="TrackerSecurityMake" type="TrackerSecurityMake" minOccurs="0"/> 
      <xs:element name="Transmission" type="Transmission" minOccurs="0"/> 
      <xs:element name="Type" type="Type" minOccurs="0"/> 
      <xs:element name="Windscreen" type="Windscreen" minOccurs="0"/> 
      <xs:element name="YearManufacture" type="YearManufacture" minOccurs="0"/> 
      <xs:element name="Modification" type="Modification" minOccurs="0" maxOccurs="5"/> 
     </xs:choice> 
    </xs:complexType> 
</xs:schema> 

Ich habe erfolgreich eingesetzt xsd.exe und xsd2code Klassen zu erzeugen, basierend auf dem Schema definiitions jedoch die Klassen, die sind nicht stark typisiert hergestellt wurden und enthalten Arrays von Gegenständen wie folgt aus:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.6.1064.2")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
public partial class Vehicle 
{ 

    private object[] _items; 

    private ItemsChoiceType4[] _itemsElementName; 

    [System.Xml.Serialization.XmlElementAttribute("ABICode", typeof(string))] 
    [System.Xml.Serialization.XmlElementAttribute("ABSBrakes", typeof(ABSBrakes))] 
    [System.Xml.Serialization.XmlElementAttribute("AgedOver", typeof(string))] 
    [System.Xml.Serialization.XmlElementAttribute("AlarmSecurityFitted", typeof(AlarmSecurityFitted))] 
    [System.Xml.Serialization.XmlElementAttribute("AlarmSecurityMake", typeof(AlarmSecurityMake))] 
    [System.Xml.Serialization.XmlElementAttribute("AudioValue", typeof(string))] 
    [System.Xml.Serialization.XmlElementAttribute("CarPhoneValue", typeof(string))] 
    [System.Xml.Serialization.XmlElementAttribute("Colour", typeof(Colour))] 
    [System.Xml.Serialization.XmlElementAttribute("CountryOfManufacture", typeof(CountryOfManufacture))] 
    [System.Xml.Serialization.XmlElementAttribute("CoverType", typeof(CoverType))] 
    [System.Xml.Serialization.XmlElementAttribute("CurrentMileage", typeof(string))] 
    [System.Xml.Serialization.XmlElementAttribute("Doors", typeof(string))] 
    [System.Xml.Serialization.XmlElementAttribute("EngineSize", typeof(string))] 
    [System.Xml.Serialization.XmlElementAttribute("EstimatedValue", typeof(string))] 
    [System.Xml.Serialization.XmlElementAttribute("Finish", typeof(Finish))] 
    [System.Xml.Serialization.XmlElementAttribute("FuelType", typeof(FuelType))] 
    [System.Xml.Serialization.XmlElementAttribute("HasSecurity", typeof(HasSecurity))] 
    [System.Xml.Serialization.XmlElementAttribute("ImmobSecurityFitted", typeof(ImmobSecurityFitted))] 
    [System.Xml.Serialization.XmlElementAttribute("ImmobSecurityMake", typeof(ImmobSecurityMake))] 
    [System.Xml.Serialization.XmlElementAttribute("Keeper", typeof(Keeper))] 
    [System.Xml.Serialization.XmlElementAttribute("Lhd", typeof(Lhd))] 
    [System.Xml.Serialization.XmlElementAttribute("Modification", typeof(Modification))] 
    [System.Xml.Serialization.XmlElementAttribute("Modified", typeof(Modified))] 
    [System.Xml.Serialization.XmlElementAttribute("NCBCountry", typeof(NCBCountry))] 
    [System.Xml.Serialization.XmlElementAttribute("NCBProtected", typeof(NCBProtected))] 
    [System.Xml.Serialization.XmlElementAttribute("NCBType", typeof(NCBType))] 
    [System.Xml.Serialization.XmlElementAttribute("NCBYears", typeof(string))] 
    [System.Xml.Serialization.XmlElementAttribute("NightLocation", typeof(NightLocation))] 
    [System.Xml.Serialization.XmlElementAttribute("OvernightPostCode", typeof(string))] 
    [System.Xml.Serialization.XmlElementAttribute("Owner", typeof(Owner))] 
    [System.Xml.Serialization.XmlElementAttribute("PermittedDrivers", typeof(PermittedDrivers))] 
    [System.Xml.Serialization.XmlElementAttribute("PricePaid", typeof(string))] 
    [System.Xml.Serialization.XmlElementAttribute("PurchaseDate", typeof(string))] 
    [System.Xml.Serialization.XmlElementAttribute("Registration", typeof(string))] 
    [System.Xml.Serialization.XmlElementAttribute("Seats", typeof(string))] 
    [System.Xml.Serialization.XmlElementAttribute("TrackerSecurityFitted", typeof(TrackerSecurityFitted))] 
    [System.Xml.Serialization.XmlElementAttribute("TrackerSecurityMake", typeof(TrackerSecurityMake))] 
    [System.Xml.Serialization.XmlElementAttribute("Transmission", typeof(Transmission))] 
    [System.Xml.Serialization.XmlElementAttribute("Type", typeof(string))] 
    [System.Xml.Serialization.XmlElementAttribute("Windscreen", typeof(Windscreen))] 
    [System.Xml.Serialization.XmlElementAttribute("YearManufacture", typeof(string))] 
    [System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemsElementName")] 
    public object[] Items 
    { 
     get 
     { 
      return this._items; 
     } 
     set 
     { 
      this._items = value; 
     } 
    } 

Idealerweise weise~~POS=HEADCOMP würde ich die Vehicle-Klasse wie eine Reihe von Eigenschaften zu haben, die Instanzen anderer Klassen sind so, dass ich kann Autoadapter verwenden, um von meinem Domänenmodell zum Anforderungsobjekt zu mappen, zu XML zu serialisieren und die Anforderung zu senden.

Aber die Art, wie diese Typen erstellt wurden, habe ich Informationen über die Struktur des Request-Objekts verloren, das ich erstellen muss. Alles muss explizit Eigenschaft für Eigenschaft zugeordnet werden. Und obwohl ich mit allen Einstellungen in xsd2code und den Switches von xsd.exe herumhantiere, kann ich nicht scheinen, dass es irgendetwas erzeugt, was ich will.

Gibt es eine Möglichkeit zu tun, was ich will, oder muss ich meinen Ansatz ändern?

Antwort

0

Ich habe ähnliche Arbeit und verwendet xsd.exe, aber in diesem Fall glaube ich, das Problem ist die Art, wie das Schema angelegt ist. Der Vehicle-Knoten ist eine unbegrenzte Liste von Elementen, bei denen es sich um einen von 40 verschiedenen Objekttypen handeln kann (ABSBrakes, AgedOver ... YearManufacture). Der Ersteller des Schemas hat Ihnen keine anderen Optionen als die Zuordnung von Eigenschaft zu Eigenschaft überlassen.

Ich weiß, dass es nicht das ist, was Sie hören möchten, aber wenn Sie über Windschutzscheiben-Daten kommen, erstellen Sie ein Windschutzscheibenobjekt und fügen Sie es Vehicle.Items hinzu und fahren Sie für jede Eigenschaft fort. Wenn Sie das serialisieren, sollte es gültiges XML erzeugen, das dem gegebenen Schema entspricht.