2016-02-04 110 views
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]並循環他的後代。 我該如何做到這一點?

+0

我需要遍歷'VAR currentShipment = shipments.ToList()[shipmentId]'這是給我一個錯誤 –

+0

我更新問題 –

+0

哪些要素將等同於shipmentId?或者如果您使用xml元素作爲索引,則將currentShipment中的var el更改爲currentShipment.Elements()中的var el。 –

回答

1

你可以通過元素的這樣的孩子的迭代:

foreach (var el in currentShipment.Elements()) 
    { 
     if (el.Name == "sourceShipmentId") 
     { 
      el.SetValue(sourceShipmentIndex); 
     } 

     if (el.Name == "shipmentIndex") 
     { 
      el.SetValue(shipmentId); 
     } 
    }