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