LINQ to XML

void Main()
{
string path = @"C:\Users\knife\Desktop\test.xml";
XDocument xml = XDocument.Load(path);
XNamespace ns = "http://tempuri.org/TracePersistence.xsd";
var events = xml.Element(ns+"TraceData").Element(ns+"Events").Elements(ns+"Event").Cast<XElement>().ToList();
var result = new List<KeyValuePair<int,string>>();
foreach(var item in events)
{
if(!item.Elements().Any(c=>c.Attribute("name").Value=="CPU"))
continue;
if(!item.Elements().Any(c=>c.Attribute("name").Value=="TextData"))
continue; result.Add(new KeyValuePair<int,string>(
int.Parse(item.Elements().FirstOrDefault(c=>c.Attribute("name").Value=="CPU").Value),
item.Elements().FirstOrDefault(c=>c.Attribute("name").Value=="TextData").Value
));
} var query = from item in result
orderby item.Key descending
select new
{
CPU = item.Key,
Statement = item.Value
};
query.Dump();
}
上一篇:MSP430F149学习之路——时钟1


下一篇:[转]关于NSAutoreleasePool' is unavailable: not available in automatic reference counting mode的解决方法