如何使用 LINQ 获取 XML 元素的位置(按顺序索引)?
我的 XML 与此格式完全相同:
<?xml version="1.0" encoding="utf-8"?>
<book>
<chapter>
<verse>This is verse 1</verse>
<verse>This is verse 2</verse>
<verse>This is verse 3</verse>
<verse>This is verse 4</verse>
</chapter>
<chapter>
<verse>This is verse 1</verse>
<verse>This is verse 2</verse>
</chapter>
<chapter>
<verse>This is verse 1</verse>
</chapter>
</book>
在使用 Linq 的 C# 中,我需要能够根据函数 state.getChapterNumber() 的值获取特定位置或索引的 XML 元素。例如,如果值为 4,我需要从 XML 文档中获取第 4 章元素。
XDocument book = XDocument.Load(string.Format("Translations/NWT/{0}.xml", state.BookName));
var verses = from chapter in book.Decendants()
where state.getChapterNumber() == (WHAT DO I PUT HERE TO MATCH THE VALUE??)
from verse in chapter.Descendants("p").Elements()
select new
{
VerseNumber = verse.Attribute("n").Value,
Text = verse.Value,
LastVerseInParagraph = verse.Parent.Elements().LastOrDefault().Value,
FirstVerseInParagraph = verse.Parent.Elements().FirstOrDefault().Value
};
请帮忙,这非常重要,并且已经困扰我好几天了。我想要做的就是能够从我在书中选择的任何章节集中获取经文。
I have XML exactly like this format:
<?xml version="1.0" encoding="utf-8"?>
<book>
<chapter>
<verse>This is verse 1</verse>
<verse>This is verse 2</verse>
<verse>This is verse 3</verse>
<verse>This is verse 4</verse>
</chapter>
<chapter>
<verse>This is verse 1</verse>
<verse>This is verse 2</verse>
</chapter>
<chapter>
<verse>This is verse 1</verse>
</chapter>
</book>
In C# using Linq, I need to be able to get the XML element of a specific position or index, based on the value of the function state.getChapterNumber(). For example, if the value were 4, I need to grab the 4th chapter element from the XML document.
XDocument book = XDocument.Load(string.Format("Translations/NWT/{0}.xml", state.BookName));
var verses = from chapter in book.Decendants()
where state.getChapterNumber() == (WHAT DO I PUT HERE TO MATCH THE VALUE??)
from verse in chapter.Descendants("p").Elements()
select new
{
VerseNumber = verse.Attribute("n").Value,
Text = verse.Value,
LastVerseInParagraph = verse.Parent.Elements().LastOrDefault().Value,
FirstVerseInParagraph = verse.Parent.Elements().FirstOrDefault().Value
};
Please help, this is really important and has been holding me back for days. All I want to do is be able to get verses from any chapter set that I choose in the book.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,我不认为你想要第二个后代 - 你想要第二个章节,这不一定是同一件事。幸运的是,这很容易做到:
编辑:对于诗节编号,使用提供元素索引的
Select
重载:我认为这对于每个诗节没有多大意义就个人而言,拥有段落中的第一节和最后一节。这并不是那节经文的真正特色,不是吗?
Well to start with, I don't think you wanted the second descendant overall - you wanted the second chapter, which isn't necessarily the same thing. Fortunately it's easy to do:
EDIT: For the verse number, use the overload of
Select
which provides the element index:I don't think it makes much sense for every verse to have the first and last verse in the paragraph, personally. It's not really a feature of that verse, is it?
尽管 Jon Skeet 的答案效率更高,但另一个正确答案是以下代码:
Another correct answer, although Jon Skeet's answer is way more efficient, is this code:
通过使用扩展方法表示法,您可以使用
Select()
调用,该调用将为您提供当前元素的索引,如下所示:之后您可以在任何以下 linq 语句中访问匿名类型的索引。
By using the extension method notation you can use the
Select()
call that will provide you the index of the current element like this:Afterwards you can than access the index of the anonymous type in any following linq statement.