Link to home
Start Free TrialLog in
Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America

asked on

Convert xml "ComplexType" snippet into a C# class / data model

Is it possible to generate a data model class in C# given this snippet?

I am looking for either automation or programmatic solutions, but I want to actually generate a C# class FROM the XML snippet!

(Specifically, given a complex type)


Something like:

public class ServiceAppointment
{
public ServiceAppointment(){}

public string AccountId;
public double ActualDuration;

...
...

etc.

}



<complexType name="ServiceAppointment">
<complexContent>
<extension base="ens:sObject">
<sequence>
<element name="Account" nillable="true" minOccurs="0" type="ens:Account"></element>
<element name="AccountId" nillable="true" minOccurs="0" type="tns:ID"></element>
<element name="ActivityHistories" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="ActualDuration" nillable="true" minOccurs="0" type="xsd:double"></element>
<element name="ActualEndTime" nillable="true" minOccurs="0" type="xsd:dateTime"></element>
<element name="ActualStartTime" nillable="true" minOccurs="0" type="xsd:dateTime"></element>
<element name="Address" nillable="true" minOccurs="0" type="tns:address"></element>
<element name="AppointmentNumber" nillable="true" minOccurs="0" type="xsd:string"></element>
<element name="ArrivalWindowEndTime" nillable="true" minOccurs="0" type="xsd:dateTime"></element>
<element name="ArrivalWindowStartTime" nillable="true" minOccurs="0" type="xsd:dateTime"></element>
<element name="AttachedContentDocuments" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="AttachedContentNotes" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="Attachments" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="City" nillable="true" minOccurs="0" type="xsd:string"></element>
<element name="CombinedAttachments" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="Contact" nillable="true" minOccurs="0" type="ens:Contact"></element>
<element name="ContactId" nillable="true" minOccurs="0" type="tns:ID"></element>
<element name="ContentDocumentLinks" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="Country" nillable="true" minOccurs="0" type="xsd:string"></element>
<element name="CountryCode" nillable="true" minOccurs="0" type="xsd:string"></element>
<element name="CreatedBy" nillable="true" minOccurs="0" type="ens:User"></element>
<element name="CreatedById" nillable="true" minOccurs="0" type="tns:ID"></element>
<element name="CreatedDate" nillable="true" minOccurs="0" type="xsd:dateTime"></element>
<element name="Description" nillable="true" minOccurs="0" type="xsd:string"></element>
<element name="DigitalSignatures" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="DueDate" nillable="true" minOccurs="0" type="xsd:dateTime"></element>
<element name="Duration" nillable="true" minOccurs="0" type="xsd:double"></element>
<element name="DurationInMinutes" nillable="true" minOccurs="0" type="xsd:double"></element>
<element name="DurationType" nillable="true" minOccurs="0" type="xsd:string"></element>
<element name="EarliestStartTime" nillable="true" minOccurs="0" type="xsd:dateTime"></element>
<element name="Emails" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="EventRelations" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="Events" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="FSL__Appointment_Grade__c" nillable="true" minOccurs="0" type="xsd:double"></element>
<element name="FSL__Auto_Schedule__c" nillable="true" minOccurs="0" type="xsd:boolean"></element>
<element name="FSL__Duration_In_Minutes__c" nillable="true" minOccurs="0" type="xsd:double"></element>
<element name="FSL__Emergency__c" nillable="true" minOccurs="0" type="xsd:boolean"></element>
<element name="FSL__FSL_Operations__r" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="FSL__GanttColor__c" nillable="true" minOccurs="0" type="xsd:string"></element>
<element name="FSL__GanttIcon__c" nillable="true" minOccurs="0" type="xsd:string"></element>
<element name="FSL__GanttLabel__c" nillable="true" minOccurs="0" type="xsd:string"></element>
<element name="FSL__Gantt_Display_Date__c" nillable="true" minOccurs="0" type="xsd:dateTime"></element>
<element name="FSL__InJeopardyReason__c" nillable="true" minOccurs="0" type="xsd:string"></element>
<element name="FSL__InJeopardy__c" nillable="true" minOccurs="0" type="xsd:boolean"></element>
<element name="FSL__InternalSLRGeolocation__Latitude__s" nillable="true" minOccurs="0" type="xsd:double"></element>
<element name="FSL__InternalSLRGeolocation__Longitude__s" nillable="true" minOccurs="0" type="xsd:double"></element>
<element name="FSL__InternalSLRGeolocation__c" nillable="true" minOccurs="0" type="tns:location"></element>
<element name="FSL__IsFillInCandidate__c" nillable="true" minOccurs="0" type="xsd:boolean"></element>
<element name="FSL__IsMultiDay__c" nillable="true" minOccurs="0" type="xsd:boolean"></element>
<element name="FSL__MDS_Calculated_length__c" nillable="true" minOccurs="0" type="xsd:double"></element>
<element name="FSL__MDT_Operational_Time__c" nillable="true" minOccurs="0" type="xsd:string"></element>
<element name="FSL__Optimization_Requests1__r" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="FSL__Optimization_Requests__r" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="FSL__Pinned__c" nillable="true" minOccurs="0" type="xsd:boolean"></element>
<element name="FSL__Prevent_Geocoding_For_Chatter_Actions__c" nillable="true" minOccurs="0" type="xsd:boolean"></element>
<element name="FSL__Related_Service__c" nillable="true" minOccurs="0" type="tns:ID"></element>
<element name="FSL__Related_Service__r" nillable="true" minOccurs="0" type="ens:ServiceAppointment"></element>
<element name="FSL__Same_Day__c" nillable="true" minOccurs="0" type="xsd:boolean"></element>
<element name="FSL__Same_Resource__c" nillable="true" minOccurs="0" type="xsd:boolean"></element>
<element name="FSL__Schedule_Mode__c" nillable="true" minOccurs="0" type="xsd:string"></element>
<element name="FSL__Schedule_over_lower_priority_appointment__c" nillable="true" minOccurs="0" type="xsd:boolean"></element>
<element name="FSL__Scheduling_Policy_Used__c" nillable="true" minOccurs="0" type="tns:ID"></element>
<element name="FSL__Scheduling_Policy_Used__r" nillable="true" minOccurs="0" type="ens:FSL__Scheduling_Policy__c"></element>
<element name="FSL__Service_Appointments__r" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="FSL__Time_Dependencies1__r" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="FSL__Time_Dependencies_2__r" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="FSL__Time_Dependencies__r" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="FSL__Time_Dependency__c" nillable="true" minOccurs="0" type="xsd:string"></element>
<element name="FSL__UpdatedByOptimization__c" nillable="true" minOccurs="0" type="xsd:boolean"></element>
<element name="FSL__Use_Async_Logic__c" nillable="true" minOccurs="0" type="xsd:boolean"></element>
<element name="FSL__Virtual_Service_For_Chatter_Action__c" nillable="true" minOccurs="0" type="xsd:boolean"></element>
<element name="FeedSubscriptionsForEntity" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="Feeds" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="GeocodeAccuracy" nillable="true" minOccurs="0" type="xsd:string"></element>
<element name="Histories" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="IsDeleted" nillable="true" minOccurs="0" type="xsd:boolean"></element>
<element name="LastModifiedBy" nillable="true" minOccurs="0" type="ens:User"></element>
<element name="LastModifiedById" nillable="true" minOccurs="0" type="tns:ID"></element>
<element name="LastModifiedDate" nillable="true" minOccurs="0" type="xsd:dateTime"></element>
<element name="LastReferencedDate" nillable="true" minOccurs="0" type="xsd:dateTime"></element>
<element name="LastViewedDate" nillable="true" minOccurs="0" type="xsd:dateTime"></element>
<element name="Latitude" nillable="true" minOccurs="0" type="xsd:double"></element>
<element name="Longitude" nillable="true" minOccurs="0" type="xsd:double"></element>
<element name="LookedUpFromActivities" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="Notes" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="NotesAndAttachments" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="OpenActivities" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="Owner" nillable="true" minOccurs="0" type="ens:sObject"></element>
<element name="OwnerId" nillable="true" minOccurs="0" type="tns:ID"></element>
<element name="ParentEntities" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="ParentRecord" nillable="true" minOccurs="0" type="ens:sObject"></element>
<element name="ParentRecordId" nillable="true" minOccurs="0" type="tns:ID"></element>
<element name="ParentRecordStatusCategory" nillable="true" minOccurs="0" type="xsd:string"></element>
<element name="ParentRecordType" nillable="true" minOccurs="0" type="xsd:string"></element>
<element name="PostalCode" nillable="true" minOccurs="0" type="xsd:string"></element>
<element name="ProcessInstances" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="ProcessSteps" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="RecordActionHistories" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="RecordActions" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="SchedEndTime" nillable="true" minOccurs="0" type="xsd:dateTime"></element>
<element name="SchedStartTime" nillable="true" minOccurs="0" type="xsd:dateTime"></element>
<element name="ServiceNote" nillable="true" minOccurs="0" type="xsd:string"></element>
<element name="ServiceReports" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="ServiceResources" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="ServiceTerritory" nillable="true" minOccurs="0" type="ens:ServiceTerritory"></element>
<element name="ServiceTerritoryId" nillable="true" minOccurs="0" type="tns:ID"></element>
<element name="State" nillable="true" minOccurs="0" type="xsd:string"></element>
<element name="StateCode" nillable="true" minOccurs="0" type="xsd:string"></element>
<element name="Status" nillable="true" minOccurs="0" type="xsd:string"></element>
<element name="StatusCategory" nillable="true" minOccurs="0" type="xsd:string"></element>
<element name="Street" nillable="true" minOccurs="0" type="xsd:string"></element>
<element name="Subject" nillable="true" minOccurs="0" type="xsd:string"></element>
<element name="SurveySubjectEntities" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="SystemModstamp" nillable="true" minOccurs="0" type="xsd:dateTime"></element>
<element name="TaskRelations" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="Tasks" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="TopicAssignments" nillable="true" minOccurs="0" type="tns:QueryResult"></element>
<element name="UserRecordAccess" nillable="true" minOccurs="0" type="ens:UserRecordAccess"></element>
<element name="WorkType" nillable="true" minOccurs="0" type="ens:WorkType"></element>
<element name="WorkTypeId" nillable="true" minOccurs="0" type="tns:ID"></element>
</sequence>
</extension>
</complexContent>
</complexType>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tom Knowlton

ASKER

This is what I get.  What I expected (wanted) was something more like a Class encapsulating access to a Data model (fields in a class) with properties like "Account" or "AccountId" or "ActivityHistories", etc.:



// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class complexType
{

    private complexTypeComplexContent complexContentField;

    private string nameField;

    /// <remarks/>
    public complexTypeComplexContent complexContent
    {
        get
        {
            return this.complexContentField;
        }
        set
        {
            this.complexContentField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string name
    {
        get
        {
            return this.nameField;
        }
        set
        {
            this.nameField = value;
        }
    }
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class complexTypeComplexContent
{

    private complexTypeComplexContentExtension extensionField;

    /// <remarks/>
    public complexTypeComplexContentExtension extension
    {
        get
        {
            return this.extensionField;
        }
        set
        {
            this.extensionField = value;
        }
    }
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class complexTypeComplexContentExtension
{

    private complexTypeComplexContentExtensionElement[] sequenceField;

    private string baseField;

    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute("element", IsNullable = false)]
    public complexTypeComplexContentExtensionElement[] sequence
    {
        get
        {
            return this.sequenceField;
        }
        set
        {
            this.sequenceField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string @base
    {
        get
        {
            return this.baseField;
        }
        set
        {
            this.baseField = value;
        }
    }
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class complexTypeComplexContentExtensionElement
{

    private string nameField;

    private bool nillableField;

    private byte minOccursField;

    private string typeField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string name
    {
        get
        {
            return this.nameField;
        }
        set
        {
            this.nameField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public bool nillable
    {
        get
        {
            return this.nillableField;
        }
        set
        {
            this.nillableField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public byte minOccurs
    {
        get
        {
            return this.minOccursField;
        }
        set
        {
            this.minOccursField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string type
    {
        get
        {
            return this.typeField;
        }
        set
        {
            this.typeField = value;
        }
    }
}

Open in new window

Then it sounds like you would rather convert from an XSD to Classes.  Visual Studio includes an XSD tool, but your presented XSD is incomplete.  You can read more about using the XSD tool here:

https://lonewolfonline.net/xsd-tool-generate-classes-xml/

-saige-
Thank you.

Eventually I was able to reference the WSDL and this became moot.

Tom