如何查询xml文件,其中我有多个具有相同名称的项目,以便我可以返回所有项目.目前我只收到第一个结果.我设法让它使用以下代码,但这将返回满足特定搜索条件的所有项目.我想要的输出是获取两个结果,例如位置是都柏林.问题是如何使用linq to xml实现这一目标
干杯克里斯,
这是代码
string location = "Oslo";
var training = (from item in doc.Descendants("item")
where item.Value.Contains(location)
select new
{
event = item.Element("event").Value,
event_location = item.Element("location").Value
}).ToList();
Run Code Online (Sandbox Code Playgroud)
xml文件如下所示
<training>
<item>
<event>C# Training</event>
<location>Prague</location>
<location>Oslo</location>
<location>Amsterdam</location>
<location>Athens</location>
<location>Dublin</location>
<location>Helsinki</location>
</item>
<item>
<event>LINQ Training</event>
<location>Bucharest</location>
<location>Oslo</location>
<location>Amsterdam</location>
<location>Helsinki</location>
<location>Brussels</location>
<location>Dublin</location>
</item>
</training>
Run Code Online (Sandbox Code Playgroud) 我有一个xml文件,如下所示.我要做的是创建一个查询,只选择具有属性"Channel"和值"Automotive"的项目.
<item>
<title>Industries</title>
<category type="Channel">Automotive</category>
<category type="Type">Cars</category>
<category type="Token">Article</category>
<category type="SpecialToken">News</category>
<guid>637f0dd7-57a0-4001-8272-f0fba60feba1</guid>
</item>
Run Code Online (Sandbox Code Playgroud)
这是我的代码
var feeds = (from item in doc.Descendants("item")
where item.Element("category").Value == "Channel"
select new { }).ToList();
Run Code Online (Sandbox Code Playgroud)
我尝试使用item.attribute方法,但我无法获取Item中的值,只有属性Value"type"
有人可以帮我解决这个问题吗?
干杯,克里斯