【发布时间】:2011-08-03 19:19:58
【问题描述】:
我正在尝试进行 Linq 查询,该查询将从 XML 文件中检索字符串值并将其放入 TextBox。我想我需要使用FirstorDefault(),但我不知道如何正确使用它。这是我目前拥有的:
var comm = from comment in xdoc.Descendants("string")
where comment.Attribute("id").Value == tagBox.Text
select comment.Element("comment").Value;
textBox.Text = comm; //Essentially
基本上,我的 XML 文件如下所示:
<root>
<string id = "STRING_ID1">
<element1 />
<comment> Some string </comment>
...
</string>
</root>
在第一个代码 sn-p 中,tagBox 引用另一个 TextBox,其中包含一个字符串,该字符串最初是从我的 string 元素的 id 属性中提取的。想法是扫描 XML 以查找匹配的 ID,然后简单地将 comment 元素的值放在 textBox 中。
【问题讨论】: