这个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();
但是所有内容都作为单个字符串进入列表的一个索引,如何将每个值转换为不同的索引?
答案 0 :(得分: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") );