我想在列表中赋值。我不明白如何在Stops节点下的List中分配值。你能告诉我我该怎么做。下面是带有类文件的代码
通过XML创建的类</ p>
[XmlRoot(ElementName="Customer")]
public class Customer {
[XmlAttribute(AttributeName="CustomerID")]
public string CustomerID { get; set; }
[XmlAttribute(AttributeName="CustomerCode")]
public string CustomerCode { get; set; }
[XmlAttribute(AttributeName="Name")]
public string Name { get; set; }
[XmlAttribute(AttributeName="Address")]
public string Address { get; set; }
}
[XmlRoot(ElementName="OrderStopPiece")]
public class OrderStopPiece {
[XmlAttribute(AttributeName="Sequence")]
public string Sequence { get; set; }
[XmlAttribute(AttributeName="PieceAction")]
public string PieceAction { get; set; }
}
[XmlRoot(ElementName="OrderStopPieces")]
public class OrderStopPieces {
[XmlElement(ElementName="OrderStopPiece")]
public OrderStopPiece OrderStopPiece { get; set; }
}
[XmlRoot(ElementName="Stop")]
public class Stop {
[XmlElement(ElementName="OrderStopPieces")]
public OrderStopPieces OrderStopPieces { get; set; }
[XmlAttribute(AttributeName="Sequence")]
public string Sequence { get; set; }
[XmlAttribute(AttributeName="StopType")]
public string StopType { get; set; }
[XmlAttribute(AttributeName="Note")]
public string Note { get; set; }
[XmlAttribute(AttributeName="Name")]
public string Name { get; set; }
}
[XmlRoot(ElementName="Stops")]
public class Stops {
[XmlElement(ElementName="Stop")]
public List<Stop> Stop { get; set; }
}
[XmlRoot(ElementName="Order")]
public class Order {
[XmlElement(ElementName="Customer")]
public Customer Customer { get; set; }
[XmlElement(ElementName="Stops")]
public Stops Stops { get; set; }
[XmlAttribute(AttributeName="UserID")]
public string UserID { get; set; }
[XmlAttribute(AttributeName="Notes")]
public string Notes { get; set; }
[XmlAttribute(AttributeName="CustomerID")]
public string CustomerID { get; set; }
}
[XmlRoot(ElementName="SaveOrder", Namespace="http://www.uBB.com/schemas/")]
public class SaveOrder {
[XmlElement(ElementName="Order")]
public Order Order { get; set; }
}
[XmlRoot(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public class Body {
[XmlElement(ElementName="SaveOrder", Namespace="http://www.uBB.com/schemas/")]
public SaveOrder SaveOrder { get; set; }
[XmlAttribute(AttributeName="UserGUID")]
public string UserGUID { get; set; }
}
[XmlRoot(ElementName="Envelope", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope {
[XmlElement(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public Body Body { get; set; }
[XmlAttribute(AttributeName="m", Namespace="http://www.w3.org/2000/xmlns/")]
public string M { get; set; }
[XmlAttribute(AttributeName="SOAP", Namespace="http://www.w3.org/2000/xmlns/")]
public string SOAP { get; set; }
}
我辅助其他值的代码
var SaveOrder = new ECSaveOrderRequest.Envelope
{
Body = new ECSaveOrderRequest.Body
{
UserGUID = guid,
SaveOrder = new ECSaveOrderRequest.SaveOrder
{
Order = new ECSaveOrderRequest.Order
{
UserID = Uid,
Notes = "",
CustomerID = "",
Customer = new ECSaveOrderRequest.Customer
{
CustomerID = "",
CustomerCode = "",
Name = "",
Address = "",
City = ""
},
Stops = new ECSaveOrderRequest.Stops()
{
Stop = new List<ECSaveOrderRequest.Stop>()
{
/* Here i want to Assign value in List<stop> */
},
},
Jobs = new ECSaveOrderRequest.Jobs()
{
Job = new ECSaveOrderRequest.Job()
{
Sequence=""
}
}
},
}
}
};
答案 0 :(得分:1)
[XmlRoot(ElementName="Stops")]
public class Stops {
[XmlElement(ElementName="Stop")]
public List<Stop> Stop { get; set; }
}
您可以定义一个列表并将其填充到您分配值的代码之外。 当列表具有所需的所有值时,可以将列表分配给属性。
Stops = new ECSaveOrderRequest.Stops()
{
Stop = listWithStopValuesYouAssignedOutsideOfThisCodeBlock
}
另一种方法是通过向列表添加新的停止点,例如
Stop = new List<ECSaveOrderRequest.Stop>()
{
new ECSaveOrderRequest.Stop() {},
new ECSaveOrderRequest.Stop() {},
},
答案 1 :(得分:0)
您可以执行以下操作。
Stops = new ECSaveOrderRequest.Stops()
{
Stop = new List<ECSaveOrderRequest.Stop>
{
new ECSaveOrderRequest.Stop {
Sequence = "",
StopType = ""
// rest of properties
},
new ECSaveOrderRequest.Stop {
Sequence = "",
StopType = ""
// rest of properties
},
}
},
但是更好(更易读)的解决方案是先创建“停止列表”,然后将其分配给Stops.Stop。
Stops = new ECSaveOrderRequest.Stops()
{
Stop = listOfStops
}