我的LINQ2XML查询工作在我的目标的一半:
var XMLDoc = XDocument.Load("WeatherData.xml"); var maximums = from tempvalue in XMLDoc.Descendants("temperature").Elements("value") where tempvalue.Parent.Attribute("type").Value == "maximum" select (string)tempvalue; var minimums = from tempvalue in XMLDoc.Descendants("temperature").Elements("value") where tempvalue.Parent.Attribute("type").Value == "minimum" select (string)tempvalue; ListMaxTemps = maximums.ToList(); List MinTemps = minimums.ToList();
但是,我在从XML文档中获取时间信息时遇到了麻烦,因为我必须匹配布局密钥信息(请参阅XML注释),我想知道在LINQ中加入这个的最佳解决方案是什么我现有查询的时间数据:
(顺便说一下,这个XML数据来自Web服务)
k-p24h-n7-1 2009-02-09T07:00:00-05:00 2009-02-09T19:00:00-05:00 2009-02-10T07:00:00-05:00 2009-02-10T19:00:00-05:00 2009-02-11T07:00:00-05:00 2009-02-11T19:00:00-05:00 2009-02-12T07:00:00-05:00 2009-02-12T19:00:00-05:00 2009-02-13T07:00:00-05:00 2009-02-13T19:00:00-05:00 2009-02-14T07:00:00-05:00 2009-02-14T19:00:00-05:00 2009-02-15T07:00:00-05:00 2009-02-15T19:00:00-05:00 k-p24h-n7-2 2009-02-08T19:00:00-05:00 2009-02-09T08:00:00-05:00 2009-02-09T19:00:00-05:00 2009-02-10T08:00:00-05:00 2009-02-10T19:00:00-05:00 2009-02-11T08:00:00-05:00 2009-02-11T19:00:00-05:00 2009-02-12T08:00:00-05:00 2009-02-12T19:00:00-05:00 2009-02-13T08:00:00-05:00 2009-02-13T19:00:00-05:00 2009-02-14T08:00:00-05:00 2009-02-14T19:00:00-05:00 2009-02-15T08:00:00-05:00 44 57 55 40 39 34 33 24 38 46 35 25 27 23
casperOne.. 6
我会先把它分解成小块.首先,我会将时间布局按摩为一个更可行的形式,按布局键分组,其中有效的开始时间和有效的结束时间相互关联:
var timeLayouts = from tempvalue in XMLDoc.Descendants("time-layout") let tempStartTimes = tempvalue.Elements("start-valid-time"). Select((x, i) => new { Index = i, ValidDateTime = (DateTime)x }) let tempEndTimes = tempvalue.Elements("end-valid-time"). Select((x, i) => new { Index = i, ValidDateTime = (DateTime)x }) select new { LayoutKey = tempvalue.Element("layout-key").Value, ValidTimeRanges = from s in tempStartTimes from e in tempEndTimes where s.Index == e.Index select new { Index = s.Index, ValidStartDateTime = s.ValidDateTime, ValidEndDateTime = e.ValidDateTime } };
然后,我会以同样的方式按摩参数:
var parameters = from tempvalue in XMLDoc.Descendants("temperature") select new { TemperatureType = (string) tempvalue.Attribute("type"), TimeLayout = (string) tempvalue.Attribute("time-layout"), Temperatures = tempvalue.Elements("value").Select((x, i) => new { Index = i, Temperature = (int)x }) };
从那里,获得最大值和最小值并不困难:
var maximums = from p in parameters where p.TemperatureType == "maximum" from tl in timeLayouts where tl.LayoutKey == p.TimeLayout from tr in tl.ValidTimeRanges from t in p.Temperatures where tr.Index == t.Index select new { tr.ValidStartDateTime, tr.ValidEndDateTime, t.Temperature }; var minimums = from p in parameters where p.TemperatureType == "minimum" from tl in timeLayouts where tl.LayoutKey == p.TimeLayout from tr in tl.ValidTimeRanges from t in p.Temperatures where tr.Index == t.Index select new { tr.ValidStartDateTime, tr.ValidEndDateTime, t.Temperature };
如果你想简化一些表示(例如,你可以将布局和参数变成更"表格式"的东西),你可以采取其他方式,这只需要一些调整.
我会先把它分解成小块.首先,我会将时间布局按摩为一个更可行的形式,按布局键分组,其中有效的开始时间和有效的结束时间相互关联:
var timeLayouts = from tempvalue in XMLDoc.Descendants("time-layout") let tempStartTimes = tempvalue.Elements("start-valid-time"). Select((x, i) => new { Index = i, ValidDateTime = (DateTime)x }) let tempEndTimes = tempvalue.Elements("end-valid-time"). Select((x, i) => new { Index = i, ValidDateTime = (DateTime)x }) select new { LayoutKey = tempvalue.Element("layout-key").Value, ValidTimeRanges = from s in tempStartTimes from e in tempEndTimes where s.Index == e.Index select new { Index = s.Index, ValidStartDateTime = s.ValidDateTime, ValidEndDateTime = e.ValidDateTime } };
然后,我会以同样的方式按摩参数:
var parameters = from tempvalue in XMLDoc.Descendants("temperature") select new { TemperatureType = (string) tempvalue.Attribute("type"), TimeLayout = (string) tempvalue.Attribute("time-layout"), Temperatures = tempvalue.Elements("value").Select((x, i) => new { Index = i, Temperature = (int)x }) };
从那里,获得最大值和最小值并不困难:
var maximums = from p in parameters where p.TemperatureType == "maximum" from tl in timeLayouts where tl.LayoutKey == p.TimeLayout from tr in tl.ValidTimeRanges from t in p.Temperatures where tr.Index == t.Index select new { tr.ValidStartDateTime, tr.ValidEndDateTime, t.Temperature }; var minimums = from p in parameters where p.TemperatureType == "minimum" from tl in timeLayouts where tl.LayoutKey == p.TimeLayout from tr in tl.ValidTimeRanges from t in p.Temperatures where tr.Index == t.Index select new { tr.ValidStartDateTime, tr.ValidEndDateTime, t.Temperature };
如果你想简化一些表示(例如,你可以将布局和参数变成更"表格式"的东西),你可以采取其他方式,这只需要一些调整.