C# 使用linq根据xml子节点的某个属性值对子节点属性进行细分
我有一个下拉列表,它包含时间节点的属性值。 我想根据父属性的值选择子节点属性 xml如下所示C# 使用linq根据xml子节点的某个属性值对子节点属性进行细分,c#,xml,linq,linq-to-xml,C#,Xml,Linq,Linq To Xml,我有一个下拉列表,它包含时间节点的属性值。 我想根据父属性的值选择子节点属性 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">
<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
请帮助我选择匹配的时间元素,然后展平子代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
谢谢你的回复,我想把name和desc存储在一些字符串中,这样我就可以绑定到GridViews了。非常感谢。我明白了。再次谢谢
var speakers = from id in xdoc.XPathSelectElements("//time[@value='0-30']/id")
select (string)id.Attribute("speaker");