0

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

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

请帮我

4

1 回答 1

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");
于 2013-06-24T10:37:23.433 回答