C# LINQ到XML的多重选择
编辑:下面是我试图解析的示例XML文档:(查看源代码) 以下是我试图检索的项目:C# LINQ到XML的多重选择,c#,linq-to-xml,C#,Linq To Xml,编辑:下面是我试图解析的示例XML文档:(查看源代码) 以下是我试图检索的项目: 标题(t正文/tr/td/a) 作者(tbody/tr/td) Url(也存储在“作者”节点中) 日期(tbody/tr/td/div/div) 答复(tbody/tr/td) 视图(也存储在上述节点中) 我会进行“预查询”,这样我就不必在下面的每个查询中进行遍历: var threads = from allThreads in xmlThreadList.Descendants(ns + "tbod
- 标题(t正文/tr/td/a)
- 作者(tbody/tr/td)
- Url(也存储在“作者”节点中)
- 日期(tbody/tr/td/div/div)
- 答复(tbody/tr/td)
- 视图(也存储在上述节点中)
var threads =
from allThreads in xmlThreadList.Descendants(ns + "tbody")
.Descendants(ns + "tr")
.Descendants(ns + "td")
select allThreads;
我有一个表示论坛线程列表的XML文档。在每个线程中都有不同的子线程,它们持有我想要检索的不同信息。目前,我通过多次查询XML文档来实现这一点。有没有一种方法可以在单个查询中提取此信息并将其存储在IEnumerable中?我现在这样做似乎效率低下
// array of xelements that contain the title and url
var threadTitles =
(from allThreads in threads.Descendants(ns + "a")
where allThreads.Parent.Attribute("class").Value.Equals("post-title")
select allThreads).ToArray();
// array of strings of author names
var threadAuthors =
(from allThreads in threads
where allThreads.Attribute("class").Value.Equals("post-author")
select allThreads.Value.Trim()).ToArray();
// ...
// there are several more queries like this
// ...
// for loop to populate a list with all the extracted data
for (int i = 0, j = 0; i < threadTitles.Length; i++, j++)
{
ThreadItem threadItem = new ThreadItem();
threadItem.Title = threadTitles[i].Value.Trim();
threadItem.Author = threadAuthors[i];
threadItem.Url = Path.Combine(_url, threadTitles[i].Attribute("href").Value);
threadItem.Date = threadDates[i];
threadItem.Replies = threadRepliesAndViews[j++];
threadItem.Views = threadRepliesAndViews[j];
_threads.Add(threadItem);
}
//包含标题和url的元素数组
变量threadTitles=
(来自threads.substands(ns+“a”)中的所有线程)
其中allThreads.Parent.Attribute(“class”).Value.Equals(“post title”)
选择所有线程)。ToArray();
//作者姓名字符串数组
变量threadAuthors=
(来自线程中的所有线程)
其中allThreads.Attribute(“class”).Value.Equals(“post-author”)
选择allThreads.Value.Trim()).ToArray();
// ...
//还有几个类似这样的查询
// ...
//for循环使用所有提取的数据填充列表
对于(int i=0,j=0;i
如有任何建议,将不胜感激。我对LINQ到XML的整个场景都不熟悉。试试类似的方法
from thread in threads
select new ThreadItem() {
Title = thread.Descendants(ns + "a").First( title => title.Parent.Attribute("class").Value.Equals("post-title")),
Date = date query part
ect....
}
这将获得一定的速度,因为您不需要一次又一次地解析整个xml块,而只需多次查看每个较小的线程,每次提取不同的信息
我很想知道哪一个结果更快,因为您正在有效地交换整个元素项适合缓存的希望,这样当您执行所有小查询时,您就可以快速访问它(在您的旧代码中)cpu上的分支预测器将根据每个长查询进行调整,以提高执行速度 试试类似的方法
from thread in threads
select new ThreadItem() {
Title = thread.Descendants(ns + "a").First( title => title.Parent.Attribute("class").Value.Equals("post-title")),
Date = date query part
ect....
}
这将获得一定的速度,因为您不需要一次又一次地解析整个xml块,而只需多次查看每个较小的线程,每次提取不同的信息
我很想知道哪一个结果更快,因为您正在有效地交换整个元素项适合缓存的希望,这样当您执行所有小查询时,您就可以快速访问它(在您的旧代码中)cpu上的分支预测器将根据每个长查询进行调整,以提高执行速度 希望这有助于:
string ns = "{http://www.w3.org/1999/xhtml}";
var doc = XDocument.Load("http://us.battle.net/wow/en/forum/1011699/");
var threads = from tr in doc.Descendants(ns + "tbody").Elements(ns + "tr")
let elements = tr.Elements(ns + "td")
let title = elements.First(a => a.Attribute("class").Value == "post-title").Element(ns + "a")
let author = elements.First(a => a.Attribute("class").Value == "post-author")
let replies = elements.First(a => a.Attribute("class").Value == "post-replies")
let views = elements.First(a => a.Attribute("class").Value == "post-views")
select new
{
Title = title.Value.Trim(),
Url = title.Attribute("href").Value.Trim(),
Author = author.Value.Trim(),
Replies = int.Parse(replies.Value),
Views = int.Parse(views.Value)
};
foreach (var item in threads)
{
Console.WriteLine(item);
}
Console.ReadLine();
希望这有助于:
string ns = "{http://www.w3.org/1999/xhtml}";
var doc = XDocument.Load("http://us.battle.net/wow/en/forum/1011699/");
var threads = from tr in doc.Descendants(ns + "tbody").Elements(ns + "tr")
let elements = tr.Elements(ns + "td")
let title = elements.First(a => a.Attribute("class").Value == "post-title").Element(ns + "a")
let author = elements.First(a => a.Attribute("class").Value == "post-author")
let replies = elements.First(a => a.Attribute("class").Value == "post-replies")
let views = elements.First(a => a.Attribute("class").Value == "post-views")
select new
{
Title = title.Value.Trim(),
Url = title.Attribute("href").Value.Trim(),
Author = author.Value.Trim(),
Replies = int.Parse(replies.Value),
Views = int.Parse(views.Value)
};
foreach (var item in threads)
{
Console.WriteLine(item);
}
Console.ReadLine();
您能提供一个您正在访问的XML的示例吗?请同时指定您要提取的确切信息。:)我现在发布了更多的信息,如果您需要更多信息,请告诉我——谢谢!您能告诉我们threads变量中的内容吗?您能提供一个您正在访问的XML的示例吗?请同时指定您要提取的确切信息。:)我现在发布了更多的信息,如果您需要更多信息,请告诉我——谢谢!你能告诉我们threads变量中有什么吗?我使用了上面的AS-CII答案,在几次运行后,比较了它们的平均时间,大致相同,但在一次调用中执行它比我使用上面的AS-CII答案的方式要干净得多,经过几次比较,他们的平均次数大致相同,但一次通话比我当时的方式要干净得多