0

这个 xml 数据对我来说有点棘手,它的结构如下:

<site time="local">
    <key>000-000-000</key>
    <valid-time period-name="Monday">2012-09-03T06:00:00-04:00</valid-time>
    <valid-time period-name="Tuesday">2012-09-04T06:00:00-04:00</valid-time>
    <valid-time period-name="Wednesday">2012-09-05T06:00:00-04:00</valid-time>
    <valid-time period-name="Thursday">2012-09-06T06:00:00-04:00</valid-time>
 </site>
 <site time="local">
    <key>123-456-789</key>
    <valid-time period-name="Monday">2012-09-03T06:00:00-04:00</valid-time>
    <valid-time period-name="Tuesday">2012-09-04T06:00:00-04:00</valid-time>
    <valid-time period-name="Wednesday">2012-09-05T06:00:00-04:00</valid-time>
    <valid-time period-name="Thursday">2012-09-06T06:00:00-04:00</valid-time>
 </site>
...more <site time="">...

我这样做是为了获取数据:

xmlCurrent = from d in Xmldata.Descendants("site")
                        .Where(s => s.Element("key").Value.Equals("123-456-789"))
                        .Select(x => new { content = x.Value})
                        .ToList();

但是所有内容都作为单个字符串进入列表的一个索引,如何将每个值放入不同的索引?

4

1 回答 1

2

您当前的代码选择<site>元素。

您可能想要更多类似的东西:

  xmlCurrent = from d in Xmldata.Descendants("site")
               where d.Element("key").Value.Equals("123-456-789")
               select d.Elements("valid-time");

那会给你一个IEnumerable<IEnumerable<XElement>>,也许你会想要把它弄平。

//untested
IEnumerable<XElement> current =  Xmldata
     .Descendants("site")
     .Where(s => s.Element("key").Value.Equals("123-456-789") )
     .SelectMany(s => s.Elements("valid-time") );
于 2012-09-03T20:22:59.823 回答