2012-06-06 71 views
0

我需要一些幫助使用LINQ到XML,我一直在閱讀在線文章,但仍然沒有運氣,任何人都可以請幫我嗎?閱讀深層次的XML標籤與C#

我只需要讀取一個XML文件,我遇到的問題是它有很多子級別,我一直無法訪問它們。

<Dias> 
    <Dia id="0"> 
    <Restricciones> 
     <Restriccion tipo="Ambiental" horaInicio="6" horaFin="10"> 
      <Placas> 
       <Placa>4</Placa> 
      </Placas> 
     </Restriccion> 
     </Restricciones> 
    </Dia> 
</Dias> 

我當前的代碼是:

var dia = (int)DateTime.Now.DayOfWeek; 

var xElement = XElement.Load("Bogota.xml"); 

var d = (from dias in xElement.Descendants("Dia") 
where dias.Attribute("id").Value == dia.ToString() 
select dias).First(); 

var rest = (from r in d.Descendants("Restricciones") 
select r); 

但我試過幾個變化,但至今沒有運氣

有人能幫忙嗎?

+0

你想出來怎麼辦?請編輯您的問題以表明這一點。 – Crisfole

+0

你試圖訪問的元素值? – Shyju

回答

0

這應該工作

var d = (from s in myXel.Descendants("Dia") 
       where s.Attribute("id").Value == dia.ToString() 
      select s).FirstOrDefault(); 

var rest = d.Descendants("Restriccion").ToList(); 
+0

我想我沒有結果,但現在它工作...非常感謝。 – evilpilaf