0
我正在嘗試遍歷列表並且無法管理它。上述通過XML元素[n]子元素迭代
public void SetShipmentsIndex(int shipmentId, string sourceShipmentIndex, IEnumerable<XElement> shipments)
{
var currentShipment = shipments.ToList()[shipmentId];
foreach (var el in currentShipment)
{
if (el.Name == "sourceShipmentId")
{
el.SetValue(sourceShipmentIndex);
}
if (el.Name == "shipmentIndex")
{
el.SetValue(shipmentId);
}
}
}
產生一個錯誤: foreach statement cannot operate on variables of type 'System.Xml.Linq.XElement' because 'System.Xml.Linq.XElement' does not contain a public definition for 'GetEnumerator'
shipments
包含3個元素。
我想要去shipments[shipmentId]
並循環他的後代。 我該如何做到這一點?
我需要遍歷'VAR currentShipment = shipments.ToList()[shipmentId]'這是給我一個錯誤 –
我更新問題 –
哪些要素將等同於shipmentId?或者如果您使用xml元素作爲索引,則將currentShipment中的var el更改爲currentShipment.Elements()中的var el。 –