尝试使用xElement搜索多个子节点时遇到问题。我的示例XML文件:
<VIDEOS>
<VIDEO>
<ID>1</ID>
<NAME>AAA</NAME>
<ACTORS>
<ACTOR>
<NAME>AAA</NAME>
<URL>www.aaa.com</URL>
</ACTOR>
<ACTOR>
<NAME>BBB</NAME>
<URL>www.bbb.com</URL>
</ACTOR>
<ACTOR>
<NAME>CCC</NAME>
<URL>www.ccc.com</URL>
</ACTOR>
</ACTORS>
</VIDEO>
<VIDEO>
<ID>2</ID>
<NAME>BBB</NAME>
<ACTORS>
<ACTOR>
<NAME>AAA</NAME>
<URL>www.aaa.com</URL>
</ACTOR>
<ACTOR>
<NAME>DDD</NAME>
<URL>www.ddd.com</URL>
</ACTOR>
<ACTOR>
<NAME>EEE</NAME>
<URL>www.eee.com</URL>
</ACTOR>
</ACTORS>
</VIDEO>
<VIDEO>
<ID>3</ID>
<NAME>CCC</NAME>
<ACTORS>
<ACTOR>
<NAME>CCC</NAME>
<URL>www.ccc.com</URL>
</ACTOR>
<ACTOR>
<NAME>BBB</NAME>
<URL>www.bbb.com</URL>
</ACTOR>
<ACTOR>
<NAME>EEE</NAME>
<URL>www.eee.com</URL>
</ACTOR>
</ACTORS>
</VIDEO>
</VIDEOS>
我试图使用:
var videos = from element in xDocument.Descendants("VIDEO").
Descendants("ACTORS").
Descendants("ACTOR").
Where(e => e.Element("NAME").Value.ToLower().Contains(searchText.ToLower()))
select new
{
Id = element.Element("ID").Value,
Name = element.Element("NAME").Value,
Actors = element.Element("ACTORS").Value
};
使用以下作品但我需要搜索所有演员名称并从节点获取所有视频详细信息。请帮忙!
var videos = from element in xDocument.Descendants("VIDEO").
Where(e => e.Element("ACTORS").Value.ToLower().Contains(searchText.ToLower()))
select new
{
Id = element.Element("ID").Value,
Name = element.Element("NAME").Value,
Actors = element.Element("ACTORS").Value
};
答案 0 :(得分:1)
试试这段代码,
static void Main(string[] args)
{
string searchText = "eee";
XDocument xDocument = XDocument.Load(@"test.xml");
var videos = from element in xDocument.Descendants("VIDEO").
Where(e => e.Descendants("ACTORS").
Descendants("ACTOR").Any(actor => actor.Element("NAME").Value.ToLower().Contains(searchText.ToLower())))
select new
{
Id = element.Element("ID").Value,
Name = element.Element("NAME").Value,
Actors = element.Element("ACTORS").Descendants("ACTOR").Select(y => new
{
name = y.Element("NAME").Value,
url = y.Element("URL").Value
})
};
}
答案 1 :(得分:0)
请尝试以下代码。如果你想要一个列表,你需要添加到结束SelectMany()。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication82
{
class Program
{
static void Main(string[] args)
{
string xml =
"<VIDEOS>" +
"<VIDEO>" +
"<ID>1</ID>" +
"<NAME>AAA</NAME>" +
"<ACTORS>" +
"<ACTOR>" +
"<NAME>AAA</NAME>" +
"<URL>www.aaa.com</URL>" +
"</ACTOR>" +
"<ACTOR>" +
"<NAME>BBB</NAME>" +
"<URL>www.bbb.com</URL>" +
"</ACTOR>" +
"<ACTOR>" +
"<NAME>CCC</NAME>" +
"<URL>www.ccc.com</URL>" +
"</ACTOR>" +
"</ACTORS>" +
"</VIDEO>" +
"<VIDEO>" +
"<ID>2</ID>" +
"<NAME>BBB</NAME>" +
"<ACTORS>" +
"<ACTOR>" +
"<NAME>AAA</NAME>" +
"<URL>www.aaa.com</URL>" +
"</ACTOR>" +
"<ACTOR>" +
"<NAME>DDD</NAME>" +
"<URL>www.ddd.com</URL>" +
"</ACTOR>" +
"<ACTOR>" +
"<NAME>EEE</NAME>" +
"<URL>www.eee.com</URL>" +
"</ACTOR>" +
"</ACTORS>" +
"</VIDEO>" +
"<VIDEO>" +
"<ID>3</ID>" +
"<NAME>CCC</NAME>" +
"<ACTORS>" +
"<ACTOR>" +
"<NAME>CCC</NAME>" +
"<URL>www.ccc.com</URL>" +
"</ACTOR>" +
"<ACTOR>" +
"<NAME>BBB</NAME>" +
"<URL>www.bbb.com</URL>" +
"</ACTOR>" +
"<ACTOR>" +
"<NAME>EEE</NAME>" +
"<URL>www.eee.com</URL>" +
"</ACTOR>" +
"</ACTORS>" +
"</VIDEO>" +
"</VIDEOS>";
XDocument doc = XDocument.Parse(xml);
var results = doc.Descendants("VIDEO").Select(x => new {
id = (int)x.Element("ID"),
name = (string)x.Element("NAME"),
actors = x.Descendants("ACTOR").Select(y => new {
name = (string)y.Element("NAME"),
url = (string)y.Element("URL")
}).ToList()
}).ToList();
}
}
}