views:

374

answers:

1

This issue is somewhat related: http://stackoverflow.com/questions/1165999/problem-with-code-generated-by-xsd-exe-sequence-of-elements-is-generated-as-an-a

Basically, I would rather work with an ArrayList in C# code then an array. I have a collection of errors, and I'm writing code to send back additional errors to the already existing collection of errors. Or if I find the first error, I have to instantiate this object, and set the first error of the array. I don't want to deal with resizing a C# array. It seems like it would be much easier to just add to an ArrayList.

I think question I referenced above was sort of asking the same thing, but in my case, I do have a complex type, not a simple one.

My schema has a field called Status which contains this:

<xs:element minOccurs="0" name="Errors">
 <xs:complexType>
   <xs:sequence>
     <xs:element minOccurs="0" maxOccurs="unbounded" name="Error">
       <xs:complexType>
         <xs:sequence>
            <xs:element name="ErrorNumber"  type="xs:string" /> 
            <xs:element name="ErrorMessage" type="xs:string" /> 
         </xs:sequence>
       </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>

I use xsd.exe to generate a C# class.

I could have made "Error" a separate schema, and referenced it (schema create with BizTalk 2006/R2), if that would make any difference in the C# generated.

The generated C# class looks like this:

private StatusError[] errorsField;

[System.Xml.Serialization.XmlArrayAttribute(Form

= System.Xml.Schema.XmlSchemaForm.Unqualified)] [System.Xml.Serialization.XmlArrayItemAttribute("Error", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)] public StatusError[] Errors { get { return this.errorsField; } set { this.errorsField = value; } } }

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.1432")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "https://firstrepublic.com/EagleConnect/Status/")] public partial class StatusError {

private string errorNumberField;

private string errorMessageField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form

= System.Xml.Schema.XmlSchemaForm.Unqualified)] public string ErrorNumber { get { return this.errorNumberField; } set { this.errorNumberField = value; } }

+1  A: 

Well, I had to continue, so I used Array.Resize. Came up with a method to look at the array, if it's not there add it, if it was there add to it, etc... An arrayList would have been easier and quicker.

 // Call the method 
    StatusError newErrorToAdd1 = new StatusError();
    newErrorToAdd1.ErrorNumber = "1112";
    newErrorToAdd1.ErrorMessage = "Demo error "; 
    transactionRequestOut = AddErrorToTransactionRequest(transactionRequestOut, newErrorToAdd1);



public static TransactionRequest AddErrorToTransactionRequest(TransactionRequest transReq, StatusError newErr)
{
        int intErrSubscript; 

        // If response is there use it, else add it 
        if (transReq.TransactionResponse == null)
        {
            TransactionResponse transactionResponse = new TransactionResponse();
            transReq.TransactionResponse = transactionResponse; 
        }

        // If response/errors are there, use them, else add them  
        if (transReq.TransactionResponse.Status == null)
        {
            Status status = new Status();
            transReq.TransactionResponse.Status = status; 
        }
        // If response/errors are there, use them, else add them  
        if (transReq.TransactionResponse.Status.Errors == null)
        {
            StatusError[] errors = new StatusError[1];
            errors[0] = new StatusError();
            intErrSubscript = 0;
            transReq.TransactionResponse.Status.Errors = errors;
        }
        else
        {
            int newArraySize = transReq.TransactionResponse.Status.Errors.Length + 1;
            intErrSubscript = newArraySize - 1;
            StatusError[] errors = transReq.TransactionResponse.Status.Errors;
            Array.Resize<StatusError> (ref errors,  newArraySize);
            transReq.TransactionResponse.Status.Errors = errors; 
        }
        transReq.TransactionResponse.Status.Errors[intErrSubscript] = newErr;
        return transReq; 
}
NealWalters