繁体   English   中英

sub子子节点的属性根据使用linq的xml子节点的某些属性值

[英]sub sub child node attributes according to some attribute value of child node of xml using linq

我有一个下拉列表,其中包含时间节点的属性值。 我想根据父属性的值选择子子子节点属性

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

请帮我

选择匹配的时间元素,然后展平后代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");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM