如何使用 LINQ 获取 XML 元素的位置(按顺序索引)?

发布于 2024-12-14 19:16:39 字数 1415 浏览 1 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青芜 2024-12-21 19:16:39

首先,我不认为你想要第二个后代 - 你想要第二个章节,这不一定是同一件事。幸运的是,这很容易做到:

// Use 1 for the second chapter, 2 for "element 2" (i.e. the third element) etc
var chapter = book.Descendants("chapter").ElementAt(1);

var verses = from verse in chapter...

编辑:对于诗节编号,使用提供元素索引的 Select 重载:

var verses = chapter.Element("verse")
                    .Select((element, index) => new {
                                VerseNumber = index + 1,
                                Text = element.Value
                            });

我认为这对于每个诗节没有多大意义就个人而言,拥有段落中的第一节和最后一节。这并不是那节经文的真正特色,不是吗?

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:

// Use 1 for the second chapter, 2 for "element 2" (i.e. the third element) etc
var chapter = book.Descendants("chapter").ElementAt(1);

var verses = from verse in chapter...

EDIT: For the verse number, use the overload of Select which provides the element index:

var verses = chapter.Element("verse")
                    .Select((element, index) => new {
                                VerseNumber = index + 1,
                                Text = element.Value
                            });

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?

苏别ゝ 2024-12-21 19:16:39

尽管 Jon Skeet 的答案效率更高,但另一个正确答案是以下代码:

chapter.ElementsBeforeSelf().Count()

Another correct answer, although Jon Skeet's answer is way more efficient, is this code:

chapter.ElementsBeforeSelf().Count()
星星的轨迹 2024-12-21 19:16:39

通过使用扩展方法表示法,您可以使用 Select() 调用,该调用将为您提供当前元素的索引,如下所示:

var indexedChapters = book.Decendants().Select((Chapter, Index) => new { Chapter, Index });

之后您可以在任何以下 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:

var indexedChapters = book.Decendants().Select((Chapter, Index) => new { Chapter, Index });

Afterwards you can than access the index of the anonymous type in any following linq statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文