我正在尝试构建一个结构结构来复制由XML序列化创建的结构。从根本上说,以下结构是准确的,当我在阅读结构时,我可以访问带有数组件的元素,如下所示。
我不能做的是弄清楚在顶层结构中创建数组然后将数组进一步放入结构的语法。
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++;
}
***更新以下是类定义:
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;
}
}
}
以及项目详情:
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;
}
答案 0 :(得分:0)
new GetListResponse Details();
不是一个有效的构造函数,但是从代码判断它看起来像orderDetails结构的名称是详细信息,所以你可能应该这样做:
new Details();
初始化结构内部的对象时,不需要指定结构的名称。
你可以做的是通过将ItemDetails和ItemDetailsField放入如下列表来更改你的GetListResponserDetailsOrder:
public partial class GetListResponserDetailsOrder
{
private string priceTypeField;
private List<GetListResponseDetailsItemDetails> ItemDetailsField;
public List<GetListResponseDetailsItemDetails> ItemDetails
{
get
{
return this.ItemDetailsField;
}
set
{
this.ItemDetailsField = value;
}
}
}
然后更改您的代码,以便您可以像这样调用构造函数:
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]);
}
您仍然可以像往常一样使用其余代码。如果要稍后在列表中添加更多元素,可以调用.Add(...),如果要删除任何元素,可以调用.Remove(...)。
答案 1 :(得分:0)
我最终使用XmlWriter创建了一个XML文档并将其序列化。这解决了这个问题。 XmlWriter具有非常快速的学习曲线,因此它似乎是产生我需要的结构的阻力最小的路径。我仍然很好奇如何在c#中实例化结构,但是满足了当前的需求。感谢所有的意见和建议。