[英]How to retrieve certain elements from xml data with Linq?
This xml data is a little tricky for me, it is structured like so: 这个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="">...
I am doing this to get the data: 我这样做是为了得到数据:
xmlCurrent = from d in Xmldata.Descendants("site")
.Where(s => s.Element("key").Value.Equals("123-456-789"))
.Select(x => new { content = x.Value})
.ToList();
But all the content is going into one index of the list as a single string, how do I get each value into a different index? 但是所有内容都作为单个字符串进入列表的一个索引,如何将每个值转换为不同的索引?
Your current code selects <site>
elements. 您当前的代码选择
<site>
元素。
You probably want something more like: 你可能想要更像的东西:
xmlCurrent = from d in Xmldata.Descendants("site")
where d.Element("key").Value.Equals("123-456-789")
select d.Elements("valid-time");
That would give you an IEnumerable<IEnumerable<XElement>>
, maybe you'll want to flatten it. 这会给你一个
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") );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.