linq to xml - 基于数组或列表显示结果
我有一个 xml 文件,我想在其中进行查询以显示其中包含某些 Id 的所有文章。这是我尝试过的,但不知何故我无法让它工作。 我环顾四周,发现的只是使用 linq to sql 的示例。
任何帮助将不胜感激...
干杯, Terry
id 在 xml 中像这样存储
<relatedcontent articleID="1, 2, 3, 4" />
这是我的 linq
var mylinks = (from item in relatedLinks.Descendants("link")
where item.Attribute("linkID").Value.Contains("1, 2")
select new
{
testlink = item.Value
});
foreach (var newarticles in mylinks)
{
Response.Write(newarticles .testlink);
}
I have a xml file on which I want to make a query to display all articles with certain Id's in it.This is what I tried but somehow I can't get it to work.
I looked around and all I could find were examples using linq to sql.
Any help would be appreciated...
Cheers,
Terry
The id's are stored like this in the xml
<relatedcontent articleID="1, 2, 3, 4" />
Here's my linq
var mylinks = (from item in relatedLinks.Descendants("link")
where item.Attribute("linkID").Value.Contains("1, 2")
select new
{
testlink = item.Value
});
foreach (var newarticles in mylinks)
{
Response.Write(newarticles .testlink);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您现在要做的是查找 Id 包含字符串“1, 2”的文章,就像 Id 为“5,8,1,2,9”一样。
你想要的可能恰恰相反;查找Id为“1, 2”的文章,即Id为“1”和“2”的文章:
What you are doing now is to look for articles where the Id contains the string "1, 2", like if the Id was "5, 8, 1, 2, 9".
What you want is probably the opposite; to find the articles where "1, 2" contains the Id, i.e. the articles with Id "1" and "2":