Question

I am trying to build a structure of structures to duplicate a structure created by an XML serialization. Fundamentally the following structure is accurate and when I am READING the structure I can access the elements with array components as shown here.

What I can't do is figure out is the syntax to CREATE the array within the top level structure and then the array further into the structure.

        GetListResponse newone = new GetListResponse ();     // this works
        newone.marker = "";                                    // these work
        newone.count = 1;
        int x = 0;
        while (reader.Read())               // This works and is where the data is coming from 
        {
            newone.orderDetails[x] = new GetListResponseDetails();         // does NOT work!
            newone.orderDetails[x].order.orderId = Convert.ToInt32(reader[4]);
            newone.orderDetails[x].order.orderPlacedTime = Convert.ToUInt64(reader[0]);
            newone.orderDetails[x].order.orderValue = Convert.ToInt32(reader[3]) * Convert.ToDecimal(reader[5]);                
            newone.orderDetails[x].order.ItemDetails[0] = new GetListResponse OrderDetailsItemDetails()[];        // this DOESN'T WORK either
            newone.orderDetails[x].order.ItemDetails[0].Price = Convert.ToDecimal(reader[5]);
            newone.orderDetails[x].order.ItemDetails[0].filledQuantity = Convert.ToInt32(reader[3]);
            newone.orderDetails[x].order.ItemDetails[0].ItemNumber = 0;
           newone.orderDetails[x].order.ItemDetails[0].orderedQuantity = Convert.ToInt32(reader[3]);
            x++;
            }

***UPDATE Here is the class definition:

    public partial class GetListResponse {

    private int countField;

    private string markerField;

    private GetListResponseDetails[] orderDetailsField;

    /// <remarks/>
    public int count {
        get {
            return this.countField;
        }
        set {
            this.countField = value;
        }
    }

    /// <remarks/>
    public string marker {
        get {
            return this.markerField;
        }
        set {
            this.markerField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute("OrderDetails", IsNullable=false)]
    public GetListResponseDetails[] orderDetails {
        get {
            return this.orderDetailsField;
        }
        set {
            this.orderDetailsField = value;
        }
    }
}

and for item details:

   public partial class GetListResponserDetailsOrder {


    private string priceTypeField;


    private GetListResponseDetailsItemDetails[] ItemDetailsField;


        public GetListResponseDetailsItemDetails [] ItemDetails {
        get {
            return this.ItemDetailsField;
        }
        set {
            this.ItemDetailsField = value;
        }
    }


}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class GetListResponseOrderDetailsItemDetails {

    private long ItemNumberField;


    private decimal orderedQuantityField;

    private decimal filledQuantityField;

    private decimal PriceField;



}
Was it helpful?

Solution

new GetListResponse Details();

Is not a valid constructor, but judging by your code it looks like the name of the orderDetails structure is Details so instead you should probably do:

new Details();

You do not need to specify the structure's name when you are initializing an object that is inside of a structure.

What you could do is change your GetListResponserDetailsOrder by making ItemDetails and ItemDetailsField into a list like so:

public partial class GetListResponserDetailsOrder
{
    private string priceTypeField;
    private List<GetListResponseDetailsItemDetails> ItemDetailsField;

    public List<GetListResponseDetailsItemDetails> ItemDetails
    {
        get
        {
            return this.ItemDetailsField;
        }
        set
        {
            this.ItemDetailsField = value;
        }
    }
}

Then change your code so you can call the constructor like this:

GetListResponse newone = new GetListResponse ();
newone.marker = "";                                    
newone.count = 1;

for (int x = 0; reader.Read(); x++)
{
    newone.orderDetails[x] = new Details();
    newone.orderDetails[x].order.orderId = Convert.ToInt32(reader[4]);
    newone.orderDetails[x].order.orderPlacedTime = Convert.ToUInt64(reader[0]);
    newone.orderDetails[x].order.orderValue = Convert.ToInt32(reader[3]) * Convert.ToDecimal(reader[5]);                
    newone.orderDetails[x].order.ItemDetails = new List<GetListResponseDetailsItemDetails>();
    newone.orderDetails[x].order.ItemDetails.Add(new GetListResponseDetailsItemDetails());
    newone.orderDetails[x].order.ItemDetails[0].Price = Convert.ToDecimal(reader[5]);
    newone.orderDetails[x].order.ItemDetails[0].filledQuantity = Convert.ToInt32(reader[3]);
    newone.orderDetails[x].order.ItemDetails[0].ItemNumber = 0;
    newone.orderDetails[x].order.ItemDetails[0].orderedQuantity = Convert.ToInt32(reader[3]);
}

You will still be able to use the rest of the code as usual. You can call .Add(...) if you want to add more elements to the list later on, or .Remove(...) if you want to remove any elements.

OTHER TIPS

I wound up creating an XML document using XmlWriter and serializing it. This solved the problem. XmlWriter has a pretty quick learning curve so it seemed like the path of least resistance to generating the structure(s) I needed. I am still curious how to instantiate the structures in c#, but the immediate need is satisfied. Thanks for all the comments and suggestions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top