使用linq将字符串分组到xml

时间:2012-04-08 11:33:24

标签: xml linq group-by

我有一些像这样的字符串:

04:51 05:09 05:29_ 05:49 06:09 06:29 06:49_ 07:09 07:25 07:40 07:55 08:10_ 08:25 08:40 08:55 09:10 09:25_ 09:40 09:55 10:10 10:25 10:40_ 10:55 11:10 11:25 11:40 11:55_ 12:10 12:25 12:40 12:55 13:10_ 13:25 13:40 13:55 14:10 14:25_ 14:40 14:55 15:10 15:25 15:40_ 15:55 16:10 16:25 16:40 16:55_ 17:10 17:29# 17:49# 18:09# 18:29#_ 18:49# 19:09# 19:29# 19:49#_ 20:09# 20:29# 20:49# 21:09#_ 21:29# 21:49# 22:09# 22:29#_ 22:51# 23:12#

我需要按小时对字符串进行分组才能得到像这样的xml:

<departure hour="04">
<min>51</min>
</departure>
<departure hour="05">
<min>09 29_ 49</min>
</departure>

etc.

我已经尝试了一些小组,但是到目前为止还没有令人满意的......

好的,现在我有这样的东西用于拆分:

 var dep = parts[2].Split(' ').Select(p => new XElement("departure",    new XAttribute("hour", p.Split(':')[0]), new XElement("min",p.Split(':')[1])));

这给了我类似的东西:

<departure hour="04">
      <min>49</min>
    </departure>
    <departure hour="05">
      <min>07</min>
    </departure>
    <departure hour="05">
      <min>27_</min>
    </departure>
    <departure hour="05">
      <min>47</min>
    </departure>

问题是分组代码......

编辑:

我设法写了这样的东西:

 var depGrouped = from grouped in dep
                  group grouped by new {h = (string)grouped.Attribute("hour")} into g 
                  select new XElement("departure", new XAttribute("hour", g.Key.h), new XElement("min", g));

但它产生的效果并不像我预期的那样:

<departure hour="05">
    <min>
      <departure hour="05">
        <min>04</min>
      </departure>
      <departure hour="05">
        <min>24</min>
      </departure>
      <departure hour="05">
        <min>44</min>
      </departure>
    </min>
  </departure>
  <departure hour="06">
    <min>
      <departure hour="06">
        <min>06_</min>
      </departure>
      <departure hour="06">
        <min>24</min>
      </departure>
      <departure hour="06">
        <min>44</min>
      </departure>
      <departure hour="06">
        <min>58</min>
      </departure>
    </min>
  </departure>

你能帮我把输出中的一些信息从帖子顶部的匹配示例中排除吗?

2 个答案:

答案 0 :(得分:1)

试试这个,在LINQPad中适合我。

var s = "04:51 05:09 05:29_ 05:49 06:09 06:29 06:49_ 07:09 07:25 07:40 07:55 08:10_ 08:25 08:40 08:55 09:10 09:25_ 09:40 09:55 10:10 10:25 10:40_ 10:55 11:10 11:25 11:40 11:55_ 12:10 12:25 12:40 12:55 13:10_ 13:25 13:40 13:55 14:10 14:25_ 14:40 14:55 15:10 15:25 15:40_ 15:55 16:10 16:25 16:40 16:55_ 17:10 17:29# 17:49# 18:09# 18:29#_ 18:49# 19:09# 19:29# 19:49#_ 20:09# 20:29# 20:49# 21:09#_ 21:29# 21:49# 22:09# 22:29#_ 22:51# 23:12#";

var times = s.Split(new char[]{' '}).Select (x => x.Trim());

var byHour = times.Select (
     t => t.Split(new char[]{':'})).Select(t => new {hour=t[0], minutes=t[1]});

var grouped = byHour.GroupBy (h => h.hour).Select (
    h => new {hour=h.Key, minutes=string.Join(" ", h.Select (x => x.minutes))});

var xml = grouped.Select (a => 
    new XElement("departure", 
    new XAttribute("hour", a.hour),
    new XElement("min", a.minutes)
    ));

xml.Dump();

答案 1 :(得分:0)

我现在没有机会尝试这个,但想法很简单:

var dep = from p in parts[2].Split(' ')
          let p_parts = p.Split(':')
          let hour = p_parts[0]
          let min = p_parts[1]
          group p by hour into g
          select new XElement("departure",
                              new XAttribute("hour", g.Key),
                              (from m in g select new XElement("min", m.min));