locked
How to return a class created by XSD.exe (array of object) RRS feed

  • Question

  • User190138249 posted

    Hi reader I am new to Web Services. I am using VS 2005,

       Would you be able to answer a query for me regarding the Web Service as I'm struggling. I was sent an schema. I ran the schema through VS xsd.exe and created a corresponding classes. I think what I then need to do is return an instance of the class in the web service? Is that right?

    The class has this structure (below). My question is how would I return an instance of the class (in the Web Service, namely traceabilityResponse?

    (TraceabilityResponse defines XML elements of other classes that were generated by the XSD.exe from the schema, i.e. LensData )

    partial class traceabilityResponse {

      

      

    private object[] itemsField;

      

      

    private ItemsChoiceType[] itemsElementNameField;

      

      

    /// <remarks/>

        [System.Xml.Serialization.

    XmlElementAttribute("lensData", typeof(LensData), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]

        [System.Xml.Serialization.

    XmlElementAttribute("prescriptionLeft", typeof(Prescription), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]

        [System.Xml.Serialization.

    XmlElementAttribute("prescriptionRight", typeof(Prescription), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]

        [System.Xml.Serialization.

    XmlElementAttribute("traceabilityError", typeof(TraceabilityError), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]

        [System.Xml.Serialization.

    XmlChoiceIdentifierAttribute("ItemsElementName")]

      

    public object[] Items {

          

    get {

              

    return this.itemsField;

            }

          

    set {

              

    this.itemsField = value;

            }

        }

      

      

    /// <remarks/>

        [System.Xml.Serialization.

    XmlElementAttribute("ItemsElementName")]

        [System.Xml.Serialization.

    XmlIgnoreAttribute()]

      

    public ItemsChoiceType[] ItemsElementName {

          

    get {

              

    return this.itemsElementNameField;

            }

          

    set {

              

    this.itemsElementNameField = value;

            }

        }

    }

    I can create instances of other classes with what the xsd.exe created from the schema i.e.:-

            LensData ld = new LensData();

            ld.ppfCode = 22;

    partial class LensData {

      

      

    private string uniqueDesignCodeField;

      

      

    private string materialCodeField;

      

      

    private string coatingCodeField;

      

      

    private string itemField;

      

      

    private ItemChoiceType itemElementNameField;

      

      

    /// <remarks/>

        [System.Xml.Serialization.

    XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]

      

    public string uniqueDesignCode {

          

    get {

              

    return this.uniqueDesignCodeField;

            }

          

    set {

              

    this.uniqueDesignCodeField = value;

            }

        }

      

      

    /// <remarks/>

        [System.Xml.Serialization.

    XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType="integer")]

      

    public string materialCode {

          

    get {

              

    return this.materialCodeField;

            }

          

    set {

              

    this.materialCodeField = value;

            }

        }

      

      

    /// <remarks/>

        [System.Xml.Serialization.

    XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType="integer")]

      

    public string coatingCode {

          

    get {

              

    return this.coatingCodeField;

            }

          

    set {

              

    this.coatingCodeField = value;

            }

        }

      

      

    /// <remarks/>

        [System.Xml.Serialization.

    XmlElementAttribute("ppfCode", typeof(string), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType="integer")]

        [System.Xml.Serialization.

    XmlElementAttribute("tintCode", typeof(string), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, DataType="integer")]

        [System.Xml.Serialization.

    XmlChoiceIdentifierAttribute("ItemElementName")]

      

    public string Item {

          

    get {

              

    return this.itemField;

            }

          

    set {

              

    this.itemField = value;

            }

        }

      

      

    /// <remarks/>

        [System.Xml.Serialization.

    XmlIgnoreAttribute()]

      

    public ItemChoiceType ItemElementName {

          

    get {

              

    return this.itemElementNameField;

            }

          

    set {

              

    this.itemElementNameField = value;

            }

        }

    }

    but not sure how to return the entire structure (which is the traceabilityResponse), any help greatly appreciated

    Any help greatly appreciated

    Thursday, September 19, 2013 7:12 AM

Answers

All replies

  • User-488622176 posted

    Do you use WCF or ASMX services? 

    Thursday, September 19, 2013 7:23 AM
  • User190138249 posted

    Hi, I'm using ASMX

    Thursday, September 19, 2013 7:32 AM
  • User-488622176 posted

    Check this sample for a tut on how to create an asmx web service : http://www.codeproject.com/Articles/863/Your-first-C-Web-Service

    In order to be able to serialize object[] properties, you'll need to provide the xml serializer with info on how to serialize. This is done by the KnownTypeAttribute. See http://stackoverflow.com/questions/3326481/c-sharp-xml-serialization-of-derived-classes.  You should add this attribute to the generated class containing the object[] proprety

    In order to return such object from the webservice implemenations, you can assign an array of specific objects tot the property as long as the specific object types are in the "KnownType" list of the attribute.

    HTH

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 19, 2013 7:44 AM
  • User190138249 posted

    Hi, thanks for the pointers. I initially created my WS using a struct but the developers who are using my WS say that the datatypes don't match the schema they provided, hence I have now added added the file to my project which was created from XSD.exe which contains the above partial classes. Can I not utilise those partial classes to utlimately return the correct datatype? They are developing WS's all day so I assume their experience/knowledge is better than mine and that's why they mentioned the schema. The schema must have some relevance and XSD.exe must assist in this process, no?

    Thursday, September 19, 2013 7:58 AM
  • User190138249 posted

    Just to reiterate,

    I have partial class generated from the schema and created by XSD.exe:-

    public partial class traceabilityResponse {        

    private object[] itemsField;        

    private ItemsChoiceType[] itemsElementNameField;        

    /// <remarks/>    

    [System.Xml.Serialization.XmlElementAttribute("lensData", typeof(LensData), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]     [System.Xml.Serialization.XmlElementAttribute("prescriptionLeft", typeof(Prescription), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]     [System.Xml.Serialization.XmlElementAttribute("prescriptionRight", typeof(Prescription), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]     [System.Xml.Serialization.XmlElementAttribute("traceabilityError", typeof(TraceabilityError), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]     [System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemsElementName")]    

    public object[] Items {   get {    return this.itemsField;   }         set {    this.itemsField = value; }     }        

    /// <remarks/>    

    [System.Xml.Serialization.XmlElementAttribute("ItemsElementName")]

    [System.Xml.Serialization.XmlIgnoreAttribute()]    

    public ItemsChoiceType[] ItemsElementName {  get { return this.itemsElementNameField; }         set {  this.itemsElementNameField = value; }     }

    }

     

    I then want to return this structure in my WS, something like:-

    [WebMethod] 

    public traceabilityResponse Traceability(string authenticationCode, string vPlusCode)

    {

    }

     

    but I need to know how to initialise & use the partial class in my WS? The partial class traceabilityResponse has xml elements made up of other class structures like LensData, Prescription etc.

     

    i'm imagining something like:-

    traceabilityResponse  tr = new traceabilityResponse(); // bu this doesn't work. Am I totally off track here?

     

    Thursday, September 19, 2013 8:34 AM
  • User190138249 posted

    I have now took a different route, the correct route I think. I have deserialized with my partial classes, that is:-

     

    XmlSerializer serializer = new XmlSerializer(typeof(traceabilityResponse));

    Stream reader = new FileStream(@"c:\sampleResponseInvalid.xml", FileMode.Open);

    traceabilityResponse TypeTraceabilityResponse = (traceabilityResponse)serializer.Deserialize(reader); 

    return TypeTraceabilityResponse;

     

    I am actually returning an XML string from a SQL Stored Proc via a function like so:-

     strXML = ClsSQL.GetTraceabilityResponse(vPlusCode);

    is there any way I can rewrite theabove from :-

    traceabilityResponse TypeTraceabilityResponse = (traceabilityResponse)serializer.Deserialize(reader);

    to:-

    traceabilityResponse TypeTraceabilityResponse = (traceabilityResponse)serializer.Deserialize(strXML);

     

    Thanks

    P

    Thursday, September 19, 2013 11:03 AM
  • User-488622176 posted

    Sure you can. See this for sample code : http://stackoverflow.com/questions/2347642/deserialize-from-string-instead-textreader

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 20, 2013 9:48 AM