我有一个下拉列表,其中包含时间节点的属性值。 我想根据父属性的值
选择子子子节点属性xml如下
<info>
<time value="0-30">
<id t_id="1" speaker="Rajesh " desc="welcome" />
<id t_id="2" speaker="Deepak " desc="to the meeting" />
</time>
<time value="31-50">
<id t_id="1" speaker="Vishal" desc="welcome" />
<id t_id="2" speaker="Vikas" desc="to the meeting" />
</time>
</info>
当我在下拉列表中选择0-30时,必须显示Rajesh和Deepak
我正在尝试使用linq
请帮帮我
答案 0 :(得分:0)
选择匹配的时间元素,然后展平后代id元素
XDocument xdoc = XDocument.Load(path_to_xml);
var speakers = xdoc.Descendants("time")
.Where(t => (string)t.Attribute("value") == "0-30")
.SelectMany(t => t.Descendants("id"))
.Select(id => (string)id.Attribute("speaker"));
查询语法
var speakers = from t in xdoc.Descendants("time")
where (string)t.Attribute("value") == "0-30"
from id in t.Descendants("id")
select (string)id.Attribute("speaker");
的XPath
var speakers = from id in xdoc.XPathSelectElements("//time[@value='0-30']/id")
select (string)id.Attribute("speaker");