0

我在下面有这个 xml 文件

<XMLBIBLE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" biblename="ENGLISHNKJ">
    <BIBLEBOOK bnumber="1" bname="Genesis">
        <CHAPTER cnumber="1">
            <VERS vnumber="1"> In the beginning God created the heavens and the earth. </VERS>
            <VERS vnumber="2"> The earth was without form, and void; and darkness [was] on the face of the deep. And the Spirit of God was hovering over the face of the waters. </VERS>
            <VERS vnumber="3"> Then God said, "Let there be light"; and there was light. </VERS>
            <VERS vnumber="4"> And God saw the light, that [it was] good; and God divided the light from the darkness. </VERS>
            <VERS vnumber="5"> God called the light Day, and the darkness He called Night. So the evening and the morning were the first day. </VERS>
            <VERS vnumber="6"> Then God said, "Let there be a firmament in the midst of the waters, and let it divide the waters from the waters." </VERS>
            <VERS vnumber="7"> Thus God made the firmament, and divided the waters which [were] under the firmament from the waters which [were] above the firmament; and it was so. </VERS>
            <VERS vnumber="8"> And God called the firmament Heaven. So the evening and the morning were the second day. </VERS>
            <VERS vnumber="9"> Then God said, "Let the waters under the heavens be gathered together into one place, and let the dry [land] appear"; and it was so. </VERS>

我想要的是像这样在 c# datagrid 上显示它,

在此处输入图像描述

截至目前,我只能获取诗句

在此处输入图像描述

这是我使用的代码。我只为演示目的获取经文,因为当我获取所有元素时,datagridview 中没有显示。

XDocument xmlDocs = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + "Bible_English_NKJV.xml");
                var q = from c in xmlDocs.Root.Descendants("BIBLEBOOK").Elements("CHAPTER")
                        select new
                        {
                            Scripture = c.Element("VERS").Value
                        };
                dgvScriptures.DataSource = q.ToList();

请。谢谢。

4

1 回答 1

1

我喜欢放入一个 DataTable,它可以很容易地绑定(或创建数据源)到 Net 对象:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Data;

namespace ConsoleApplication180
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("BibleName", typeof(string));
            dt.Columns.Add("Reference", typeof(string));
            dt.Columns.Add("Scriptyure", typeof(string));

            XDocument doc = XDocument.Load(FILENAME);
            XElement xmlBible = doc.Descendants("XMLBIBLE").FirstOrDefault();
            string bibleName = (string)xmlBible.Attribute("biblename");
            XElement bibleBook = xmlBible.Element("BIBLEBOOK");
            string bookname = (string)bibleBook.Attribute("bname");

            foreach (XElement chapter in bibleBook.Elements("CHAPTER"))
            {
                string cnumber = (string)chapter.Attribute("cnumber");
                foreach (XElement vers in chapter.Elements("VERS"))
                {
                    string vnumber = (string)vers.Attribute("vnumber");
                    string scripture = (string)vers;
                    string reference = string.Format("{0} {1}.{2}", bookname,cnumber, vnumber);

                    dt.Rows.Add(new object[] { bibleName, reference, scripture });
                }
            }
          }
    }
}
于 2021-02-04T11:08:02.393 回答