locked
Using serialized class generated by xsd.exe RRS feed

  • Question

  • Hello all

    I need to implement next scenario. Receive message from SQL adapter, map message to excel file and send it via email.

    For excel generating I am using custom assembly which receives XLANGMessage as input and returns XLANGMessage with binary data.

    My orchestration receives message from SQL Adapter and calls assembly providing message as input.

    The ideas is to map SQL message into excel file inside custom assembly. To avoid using XPATH I have generated C# class representing SQL message using xsd.exe based on schema generated by SQLAdapter.

    I have done it in this way:

    xsd /c /n:"http://SQLGetOrder" "C:\ORDER.xsd"
    The generated class could be successfully serialized from XLANMessage. The message has 2 level structure (head and nested details loop). But the problem is that I can handle only head records and can't get access to nested level.

    I am trying to get nested records in this way:
    OPType[] type = (OPType[])order.OH[0].Items[(int)ItemsChoiceType2.OP];

    But receiving error:
    Inner exception: Unable to cast object of type 'System.String' to type 'SQLGetOrder.OPType[]'.

    Could you please explain how to get nested records from serialized class?

    Tuesday, December 15, 2009 9:58 AM

Answers

  • I have solved the issue by modifying the xsd schema generated by SQL Adapter.
    For each repeting node you should set value "Group Order Type" = "Sequence" instead of default "Choise".

    After this the generated class fields would be accesible via dot:

    String OrderNo = order.HEAD[0].ORDER_NO.ToString();
    for (int i = 0; i < order.HEAD[0].POSITIONS.Length; i++){
     String Item = order.HEAD[0].POSITIONS[i].ITEM[0].ToString();
    }

     

     

    • Marked as answer by Alexander Dik Tuesday, December 15, 2009 3:08 PM
    Tuesday, December 15, 2009 3:05 PM

All replies

  • Hi,

    Why are you making it so difficult for yourself? There are pipelinecomponents (I think even free ones) that create excel files for you.
    Or do you have a good reason for this?

    Could you give a little bit more of your code?

    Kind Regards
    Tim D'haeyer
    Tuesday, December 15, 2009 12:28 PM
  • We are using non-free framework which builds Excel reports. It permits map values to excel as to class variables. So the ultimate goal is to map excel in the next way:

    Excel.OrderNo = Message.OrderNo;
    Excel.Date = Message.Date;
    ...

    I think it is the simplest way.

    Actually I could do it all inside pipeline instead of custom assembly, but nevertheless I want to use serialized instance of object to make mapping as simple as possible.

    Or may be you want to tell that the Pipeline provide some kind of simple serialization? As far as I know it just returns byte[] so the serialization problem still exists:

    How to make entire message strucuture available via serialized class variables?

    I am doing it in pretty simple way:

    public void BuildReport(XLANGMessage InMes,XLANGMessage OutMes)
    {
     ORDER order = (ORDER)mes[0].RetrieveAs(typeof(ORDER));

     String OrderNo = order.OH[0].Items[(int)ItemsChoiceType2.ORDER_NO].ToString(); //It works fine

     OPType[] op = (OPType[])order.OH[0].Items[(int)ItemsChoiceType2.OP]; //And this one fails
     ...

     OutMes.LoadFrom(new XLANGMesBuildHelper(ReportStream.GetBuffer()));
    }

    Tuesday, December 15, 2009 1:51 PM
  • can you post the error.

    iam not aware of your schema but may be that item needs to be instantiated.(create an instance and use)
    KiranMP
    Tuesday, December 15, 2009 2:03 PM
  • Instance is created (order variable), but I can access only head level. And when trying to access nested level it throws error:
    Unable to cast object of type 'System.String' to type 'SQLGetOrder.OPType[]'.
    Tuesday, December 15, 2009 2:11 PM
  • Hi Alexander,

    How do you cast your XML instance to an object? Do you use the XmlSerializer class?
    Because in an expression shape you can just do an assign from an xpath like this

    MyObject=xpath(Msg,'root');

    Or something like that. That will automatically deserialise the xml into an object.

    Kind Regards
    Tim D'haeyer
    Tuesday, December 15, 2009 2:16 PM
  • create an instance of item also and try I guess item is repeatable record in schema

    (
    order.OH[0].Items[] items = order.OH[0].Items[];
    )
    KiranMP
    Tuesday, December 15, 2009 2:17 PM
  • I have solved the issue by modifying the xsd schema generated by SQL Adapter.
    For each repeting node you should set value "Group Order Type" = "Sequence" instead of default "Choise".

    After this the generated class fields would be accesible via dot:

    String OrderNo = order.HEAD[0].ORDER_NO.ToString();
    for (int i = 0; i < order.HEAD[0].POSITIONS.Length; i++){
     String Item = order.HEAD[0].POSITIONS[i].ITEM[0].ToString();
    }

     

     

    • Marked as answer by Alexander Dik Tuesday, December 15, 2009 3:08 PM
    Tuesday, December 15, 2009 3:05 PM