在这种情况下我如何使用LINQ2XML?

时间:2009-02-12 17:26:54

标签: c# .net xml linq linq-to-xml

我的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;

List<string> MaxTemps = maximums.ToList();
List<string> MinTemps = minimums.ToList();

但是,我无法从XML文档中获取时间信息,因为我必须匹配布局密钥信息(请参阅XML注释),我想知道什么是LINQ中最好的解决方案将此时间数据与我现有的查询相关联:

(顺便提一下,此XML数据来自网络服务)

<?xml version="1.0" encoding="utf-8"?>
<dwml>
  <data>
    <time-layout>
      <!--        Maximums Key         -->
      <layout-key>k-p24h-n7-1</layout-key>
      <start-valid-time>2009-02-09T07:00:00-05:00</start-valid-time>
      <end-valid-time>2009-02-09T19:00:00-05:00</end-valid-time>
      <start-valid-time>2009-02-10T07:00:00-05:00</start-valid-time>
      <end-valid-time>2009-02-10T19:00:00-05:00</end-valid-time>
      <start-valid-time>2009-02-11T07:00:00-05:00</start-valid-time>
      <end-valid-time>2009-02-11T19:00:00-05:00</end-valid-time>
      <start-valid-time>2009-02-12T07:00:00-05:00</start-valid-time>
      <end-valid-time>2009-02-12T19:00:00-05:00</end-valid-time>
      <start-valid-time>2009-02-13T07:00:00-05:00</start-valid-time>
      <end-valid-time>2009-02-13T19:00:00-05:00</end-valid-time>
      <start-valid-time>2009-02-14T07:00:00-05:00</start-valid-time>
      <end-valid-time>2009-02-14T19:00:00-05:00</end-valid-time>
      <start-valid-time>2009-02-15T07:00:00-05:00</start-valid-time>
      <end-valid-time>2009-02-15T19:00:00-05:00</end-valid-time>
    </time-layout>
    <time-layout>
      <!--        Minimums Key         -->
      <layout-key>k-p24h-n7-2</layout-key>
      <start-valid-time>2009-02-08T19:00:00-05:00</start-valid-time>
      <end-valid-time>2009-02-09T08:00:00-05:00</end-valid-time>
      <start-valid-time>2009-02-09T19:00:00-05:00</start-valid-time>
      <end-valid-time>2009-02-10T08:00:00-05:00</end-valid-time>
      <start-valid-time>2009-02-10T19:00:00-05:00</start-valid-time>
      <end-valid-time>2009-02-11T08:00:00-05:00</end-valid-time>
      <start-valid-time>2009-02-11T19:00:00-05:00</start-valid-time>
      <end-valid-time>2009-02-12T08:00:00-05:00</end-valid-time>
      <start-valid-time>2009-02-12T19:00:00-05:00</start-valid-time>
      <end-valid-time>2009-02-13T08:00:00-05:00</end-valid-time>
      <start-valid-time>2009-02-13T19:00:00-05:00</start-valid-time>
      <end-valid-time>2009-02-14T08:00:00-05:00</end-valid-time>
      <start-valid-time>2009-02-14T19:00:00-05:00</start-valid-time>
      <end-valid-time>2009-02-15T08:00:00-05:00</end-valid-time>
    </time-layout>
    <parameters>
      <!--                                     1st Key   -->
      <temperature type="maximum" time-layout="k-p24h-n7-1">
        <value>44</value>
        <value>57</value>
        <value>55</value>
        <value>40</value>
        <value>39</value>
        <value>34</value>
        <value>33</value>
      </temperature>
      <!--                                     2nd Key   -->
      <temperature type="minimum" time-layout="k-p24h-n7-2">
        <value>24</value>
        <value>38</value>
        <value>46</value>
        <value>35</value>
        <value>25</value>
        <value>27</value>
        <value>23</value>
      </temperature>
    </parameters>
  </data>
</dwml>

1 个答案:

答案 0 :(得分: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 };

如果你想简化一些表示(例如,你可以将布局和参数变成更“表格式”的东西),你可以采用其他方法,这只需要一些调整。< / p>