简体   繁体   English

xsd.exe转换为C#-以任何方式获取ArrayList而不是Array?

[英]xsd.exe to C# - any way to get an ArrayList instead of an Array?

This issue is somewhat related: Problem with Code Generated by XSD.EXE: Sequence of Elements is Generated as an Array 此问题有些相关: XSD.EXE生成的代码有问题:元素序列生成为数组

Basically, I would rather work with an ArrayList in C# code then an array. 基本上,我宁愿使用C#代码中的ArrayList然后使用数组。 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. 我不想处理调整C#数组的大小。 It seems like it would be much easier to just add to an ArrayList. 似乎仅将其添加到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: 我的架构有一个名为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>

I use xsd.exe to generate a C# class. 我使用xsd.exe生成C#类。

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. 如果可以在生成的C#中产生任何差异,则可以将“错误”设置为单独的架构,并对其进行引用(使用BizTalk 2006 / R2创建架构)。

The generated C# class looks like this: 生成的C#类如下所示:

private StatusError[] errorsField; 私人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; = System.Xml.Schema.XmlSchemaForm.Unqualified)] [System.Xml.Serialization.XmlArrayItemAttribute(“ Error”,Form = System.Xml.Schema.XmlSchemaForm.Unqualified,IsNullable = false)] public StatusError []错误{返回this.errorsField; } set { this.errorsField = value; }设置{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 { [System.CodeDom.Compiler.GeneratedCodeAttribute(“ xsd”,“ 2.0.50727.1432”)] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute(“ code”)] [系统.Xml.Serialization.XmlTypeAttribute(AnonymousType = true,Namespace =“ https://firstrepublic.com/EagleConnect/Status/ ”)]公共部分类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; = System.Xml.Schema.XmlSchemaForm.Unqualified)]公共字符串ErrorNumber {get {return this.errorNumberField; } set { this.errorNumberField = value; }设置{this.errorNumberField = value; } } }}

Well, I had to continue, so I used Array.Resize. 好吧,我必须继续,所以我使用了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. 想出了一种方法来查看数组,如果不存在,则添加它,如果存在,则添加到数组中,等等。。。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; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM