2009-11-05 130 views
0

我有一個加載到XElement的XML文件。我想要計算既存在又滿足一些條件的PollEvent子項的數量。我已經得到了代碼來計算PollEvent的總數,但是當我來過濾這個列表時,我似乎無法調用。在這裏,我相信這是由於.elements返回(擴展名)IEnumerable<XElements>XElement.Elements()擴展方法?

這是迄今爲止的代碼 - 我已經將下一個想要發生的事情放在評論中。

public PollDayPoint LoadData(DateTime date) 
    { 
     PollDayPoint newDayPoint = new PollDayPoint(); 
     newDayPoint.Date = date; 

     String filename = AlertLogging.CreateFileName(date); 

     XElement xmlDoc = XElement.Load(filename); 

     IEnumerable<XElement> sdis = xmlDoc.Elements("SDI"); 
     IEnumerable<XElement> pollEvents = sdis.Elements("PollEvent"); 

     //This works 
     int count = 0; 
     using (IEnumerator<XElement> enumerator = pollEvents.GetEnumerator()) 
      while (enumerator.MoveNext()) 
       count++; 

     newDayPoint.NumPolls = count; 

     //Now we want to get all poll events that match certain conditions 
     //Match those that have "Alert" children with attribute status="Sent" 

     //IEnumerable<XElement> alertEvents = sdis.Elements("PollEvent"); 

     return newDayPoint; 
    } 

下面

<PollEvents> 
    <SDI address="10.1.12.52"> 
    <PollEvent time="2009_4_11 13:51:49"> 
     <Alert errorCode="System.Net.WebException" errormessage="The remote server returned an error: (504) Gateway Timeout." status="Sent" handletime="2009_4_11 13:51:49" /> 
    </PollEvent> 
    <PollEvent time="2009_4_11 14:06:47"> 
     <Alert errorCode="System.Net.WebException" errormessage="The remote server returned an error: (504) Gateway Timeout." status="Supressed" handletime="2009_4_11 14:06:47" /> 
    </PollEvent> 
    <PollEvent time="2009_4_11 14:21:47"> 
     <Alert errorCode="System.Net.WebException" errormessage="The remote server returned an error: (504) Gateway Timeout." status="Supressed" handletime="2009_4_11 14:21:47" /> 
    </PollEvent> 
    </SDI> 
</PollEvents> 

回答

3

一些示例XML一開始,你的計數簡單的實現爲:

int count = pollEvents.Count(); 

但是,你應該能夠得到Elements()通過extension method工作,你應該可以打電話Where沒有問題,像這樣:

var alertEvents = 
    sdis.Elements("PollEvent") 
     .Where(x => x.Elements("Alert") 
        .Any(alert => (string) alert.Attribute("status") == "Sent") 
      ); 

如果這不起作用,你可以發佈你遇到的錯誤嗎?

+1

謝謝 - 沒有錯誤,數只不存在作爲IEnumerable 的方法,但我意識到我是缺少System.Linq作爲參考!衛生署! – Chris 2009-11-05 13:45:42

0

使用LINQ查詢表達式來處理這些:

var result = from el in XElement.Parse(data).Descendants("PollEvent") 
      where el.Element("Alert").Attribute("status").Value == "Sent" 
      select el; 


newDayPoint.NumPolls = result.Count(); 
1

擴展Jon的答案,這將是他的解決方案的擴展方法:

namespace MyExtensions 
{ 
    public static class XElementsExtension 
    { 
     //Returns an IEnumerable of <PollEvent> having an "<Alert>" child element with attribute "status" == status 
     public static IEnumerable<XElement> FindElementsByStatus(this IEnumerable<XElement> list, string status) 
     { 
      return list.Elements("PollEvent") 
         .Where(x => x.Elements("Alert") 
            .Any(alert => (string)alert.Attribute("status") == status) 
          ); 
     } 
    } 
} 
+0

Ahh yes擴展會很酷,謝謝 – Chris 2009-11-09 11:44:09