2013-06-26 122 views
0

有人可以幫助我使用正確的LINQ語法從這個XML中提取狀態嗎?C#XML LINQ屬性的屬性

理想情況下,我想有以下打印出來:

狀態:打開= 1

狀態:掛起= 2種

狀態:忽略= 3種

狀態:關閉= 4

<?xml version="1.0" encoding="UTF-8"?> 
<configuration> 
    <scope name="com"> 
    <scope name="company"> 
     <scope name="app"> 
     <scope name="app_monitor"> 
      <scope name="statuses"> 
      <entry name="Open">1</entry> 
      <entry name="Pending">2</entry> 
      <entry name="Ignored">3</entry> 
      <entry name="Closed">4</entry> 
      </scope> 
      <scope name="urgencies"> 
      <entry name="Critical">1</entry> 
      <entry name="Alarm">2</entry> 
      <entry name="Info">3</entry> 
      </scope>      
     </scope> 
     </scope> 
    </scope> 
    </scope> 
</configuration> 

我嘗試過不同的變化,但是h我是多遠:

XDocument Xdocument = new XDocument(); 
    var doc = XDocument.Load(@"c:\temp\app_sett.xml"); 
    var returnedvalues = from app_sett in doc.Descendants("scope") 
       where app_sett.Attribute("name").Value == "statuses" 
       select new         
       { 
        blah = app_sett.Attribute("name").Value, 
       }; 

回答

2

這聽起來像你需要得到相關的作用域元素的子元素。例如:

var query = doc.Descendants("scope") 
       .Where(x => (string) x.Attribute("name") == "statuses") 
       .Elements("entry") 
       .Select(entry => new { Name = (string) entry.Attribute("name"), 
             Value = (int) entry }); 
+0

輝煌。謝謝。!!!!!!!! – user2437909