xsd.exe到C# - 获取ArrayList而不是Array的任何方法?

时间:2010-03-10 18:05:01

标签: c# xsd xml-serialization

这个问题有点相关: Problem with Code Generated by XSD.EXE: Sequence of Elements is Generated as an Array

基本上,我宁愿在C#代码中使用ArrayList,然后使用数组。我有一组错误,我正在编写代码以将其他错误发送回已经存在的错误集合。或者,如果我发现第一个错误,我必须实例化此对象,并设置数组的第一个错误。我不想处理调整C#数组的大小。似乎只是添加到ArrayList会容易得多。

我认为上面提到的问题有点问同样的问题,但就我而言,我确实有一个复杂的类型,而不是一个简单的类型。

我的架构有一个名为Status的字段,其中包含:

<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>

我使用xsd.exe生成C#类。

我本可以将“Error”作为一个单独的模式,并引用它(使用BizTalk 2006 / R2创建模式),如果这会对生成的C#产生任何影响。

生成的C#类如下所示:

  

private StatusError [] errorsField;

[System.Xml.Serialization.XmlArrayAttribute(Form
     

= System.Xml.Schema.XmlSchemaForm.Unqualified)]       [System.Xml.Serialization.XmlArrayItemAttribute( “错误”,   表格=   System.Xml.Schema.XmlSchemaForm.Unqualified,   IsNullable = false)]       public StatusError []错误       {           得到           {               return this.errorsField;           }           组           {               this.errorsField = value;           }       }}

     

[System.CodeDom.Compiler.GeneratedCodeAttribute( “XSD”,   “2.0.50727.1432”)]   [System.SerializableAttribute()]   [System.Diagnostics.DebuggerStepThroughAttribute()]   [System.ComponentModel.DesignerCategoryAttribute( “代码”)]   [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       {           得到           {               return this.errorNumberField;           }           组           {               this.errorNumberField = value;           }       }

1 个答案:

答案 0 :(得分:1)

好吧,我必须继续,所以我使用了Array.Resize。想出了一个查看数组的方法,如果它没有添加它,如果有添加它等等...一个arrayList会更容易和更快。

 // 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; 
}