如何选择节点的所有子节点,而不是所有的后代节点?

时间:2022-06-09 22:51:04

I've got the following XML:

我有以下XML:

<...>
  <...>
    <tabular>
        <time from="2014-05-22T10:00:00" to="2014-05-22T12:00:00" period="1">
          <symbol number="3" numberEx="3" var="03d" />
          <precipitation value="0" />
          <windDirection deg="191.8" code="SSW" />
          <windSpeed mps="1.3" />
          <temperature unit="celsius" value="16" />
          <pressure unit="hPa" value="1010.6" />
        </time>
        <time from="2014-05-22T10:00:00" to="2014-05-22T12:00:00" period="1">
          <symbol number="3" numberEx="3" var="03d" />
          <precipitation value="0" />
          <windDirection deg="191.8" code="SSW" />
          <windSpeed mps="1.3" />
          <temperature unit="celsius" value="16" />
          <pressure unit="hPa" value="1010.6" />
        </time>
        <time from="2014-05-22T10:00:00" to="2014-05-22T12:00:00" period="1">
          <symbol number="3" numberEx="3" var="03d" />
          <precipitation value="0" />
          <windDirection deg="191.8" code="SSW" />
          <windSpeed mps="1.3" />
          <temperature unit="celsius" value="16" />
          <pressure unit="hPa" value="1010.6" />
        </time>

I've managed with LINQ to get the tabular list:

我已经和LINQ取得了列表:

var tabular = doc.Root.Descendants("tabular").ToList();

tabular count is now 1. What I want is a list of children of tabular. I've tried the extra descendant:

表格计数现在是1。我想要的是一张列表。我已经尝试了额外的后代:

var tabular = doc.Root.Descendants("tabular").Descendants().ToList();

But that return a list of every descendant of tabular, resulting in a list where time, symbol, precipitation and so on is in the list. How can I get a list where the list contains time nodes of tabular?

但是它会返回表的每个后代的列表,从而产生一个列表,其中的时间、符号、降水等等都在列表中。我如何得到一个列表,其中列表包含表格的时间节点?

I want a list of all time nodes, so I can serialize it to a object and access the values

我想要一个所有时间节点的列表,这样我就可以将它序列化为对象并访问值

1 个解决方案

#1


4  

Use Elements instead of Descendants if you want to get only direct children of tabular

如果希望只获得表格的直接子元素,则使用元素而不是子代元素

var tabular = doc.Root.Descendants("tabular").Elements().ToList();

UPDATE: Hints for parsing time elements into objects

更新:将时间元素解析为对象的提示

var times =
    from t in xdoc.Descendants("tabular").Elements()
    let symbol = t.Element("symbol")
    let temperature = t.Element("temperature")
    select new
    {
        From = (DateTime)t.Attribute("from"),
        To = (DateTime)t.Attribute("to"),
        Period = (int)t.Attribute("period"),
        Symbol = new
        {
            Number = (int)symbol.Attribute("number"),
            NumberEx = (int)symbol.Attribute("numberEx"),
            Var = (string)symbol.Attribute("var")
        },
        Precipitation = (int)t.Element("precipitation").Attribute("value"),
        WindSpeed = (double)t.Element("windSpeed").Attribute("mps"),
        Temperature = new
        {
            Unit = (string)temperature.Attribute("unit"),
            Value = (string)temperature.Attribute("value")
        }
    };

Output:

输出:

[
  {
    From: "2014-05-22T10:00:00",
    To: "2014-05-22T12:00:00",
    Period: 1,
    Symbol: { Number: 3, NumberEx: 3, Var: "03d" },
    Precipitation: 0,
    WindSpeed: 1.3,
    Temperature: { Unit: "celsius", Value: "16" }
  },
  // ...
]

#1


4  

Use Elements instead of Descendants if you want to get only direct children of tabular

如果希望只获得表格的直接子元素,则使用元素而不是子代元素

var tabular = doc.Root.Descendants("tabular").Elements().ToList();

UPDATE: Hints for parsing time elements into objects

更新:将时间元素解析为对象的提示

var times =
    from t in xdoc.Descendants("tabular").Elements()
    let symbol = t.Element("symbol")
    let temperature = t.Element("temperature")
    select new
    {
        From = (DateTime)t.Attribute("from"),
        To = (DateTime)t.Attribute("to"),
        Period = (int)t.Attribute("period"),
        Symbol = new
        {
            Number = (int)symbol.Attribute("number"),
            NumberEx = (int)symbol.Attribute("numberEx"),
            Var = (string)symbol.Attribute("var")
        },
        Precipitation = (int)t.Element("precipitation").Attribute("value"),
        WindSpeed = (double)t.Element("windSpeed").Attribute("mps"),
        Temperature = new
        {
            Unit = (string)temperature.Attribute("unit"),
            Value = (string)temperature.Attribute("value")
        }
    };

Output:

输出:

[
  {
    From: "2014-05-22T10:00:00",
    To: "2014-05-22T12:00:00",
    Period: 1,
    Symbol: { Number: 3, NumberEx: 3, Var: "03d" },
    Precipitation: 0,
    WindSpeed: 1.3,
    Temperature: { Unit: "celsius", Value: "16" }
  },
  // ...
]