Noob LINQ - 使用 XDocument 读取、过滤 XML

发布于 2024-08-28 23:54:53 字数 1768 浏览 2 评论 0原文

我刚刚学习 XDocument 和 LINQ 查询。这是一些简单的 XML(在我的浏览器中,该论坛的格式看起来不太正确,但您明白了...)

<?xml version="1.0" encoding="utf-8"?>
<quiz  
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.example.com/name XMLFile2.xsd"
  title="MyQuiz1">
  <q_a>
    <q_a_num>1</q_a_num>
    <q_>Here is question 1</q_>
    <_a>Here is the answer to 1</_a>
  </q_a>

  <q_a>
    <q_a_num>2</q_a_num>
    <q_>Here is question 2</q_>
    <_a>Here is the answer to 2</_a>
  </q_a>
</quiz>

我可以迭代 XML 文件中的所有元素,并在一个文件中显示它们的名称、值和 NodeType。像这样的 ListBox,没问题:

        XDocument doc = XDocument.Load(sPath);
        IEnumerable<XElement> elems = doc.Descendants();

        IEnumerable<XElement> elem_list = from elem in elems
                                          select elem;

        foreach (XElement element in elem_list)
        {
            String str0 = "Name = " + element.Name.ToString() + 
                          ",  Value = " + element.Value.ToString() +
                          ",  Nodetype = " + element.NodeType.ToString();
            System.Windows.Controls.Label strLabel = new System.Windows.Controls.Label();
            strLabel.Content = str0;
            listBox1.Items.Add(strLabel);
        }

...但是现在我想在查询中添加一个“where”子句,以便我只选择具有特定名称(例如“qa”)的元素,但我的元素列表显示为空。我试过 。 。 。

            IEnumerable<XElement> elem_list = from elem in elems
                                        where elem.Name.ToString() == "qa"
                                        select elem;

有人可以解释一下我做错了什么吗? (一般来说,有一些调试查询的好技巧吗?)提前致谢!

I'm just learning XDocument and LINQ queries. Here's some simple XML (which doesn't look formatted exactly right in this forum in my browser, but you get the idea . . .)

<?xml version="1.0" encoding="utf-8"?>
<quiz  
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.example.com/name XMLFile2.xsd"
  title="MyQuiz1">
  <q_a>
    <q_a_num>1</q_a_num>
    <q_>Here is question 1</q_>
    <_a>Here is the answer to 1</_a>
  </q_a>

  <q_a>
    <q_a_num>2</q_a_num>
    <q_>Here is question 2</q_>
    <_a>Here is the answer to 2</_a>
  </q_a>
</quiz>

I can iterate across all elements in my XML file and display their Name, Value, and NodeType in a ListBox like this, no problem:

        XDocument doc = XDocument.Load(sPath);
        IEnumerable<XElement> elems = doc.Descendants();

        IEnumerable<XElement> elem_list = from elem in elems
                                          select elem;

        foreach (XElement element in elem_list)
        {
            String str0 = "Name = " + element.Name.ToString() + 
                          ",  Value = " + element.Value.ToString() +
                          ",  Nodetype = " + element.NodeType.ToString();
            System.Windows.Controls.Label strLabel = new System.Windows.Controls.Label();
            strLabel.Content = str0;
            listBox1.Items.Add(strLabel);
        }

...but now I want to add a "where" clause to my query so that I only select elements with a certain name (e.g., "qa") but my element list comes up empty. I tried . . .

            IEnumerable<XElement> elem_list = from elem in elems
                                        where elem.Name.ToString() == "qa"
                                        select elem;

Could someone please explain what I'm doing wrong? (and in general are there some good tips for debugging Queries?) Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

蓝色星空 2024-09-04 23:54:53

问题是 Name 属性不是字符串,而是 XName.当你ToString它时,你得到的东西比你想象的要多得多。

虽然可以按照您尝试的方式编写查询,但也要考虑以下可能性:

//from nodes immediately below this one
IEnumerable<XElement> elem_list = doc.Elements("qa");

//from nodes of all levels below this node.
IEnumerable<XElement> elem_list = doc.Descendants("qa");

The problem is that the Name property is not a string, it's an XName. When you ToString it, you get a lot more than you think.

While it's possible to write the query in the way you're attempting to, also consider these possibilites:

//from nodes immediately below this one
IEnumerable<XElement> elem_list = doc.Elements("qa");

//from nodes of all levels below this node.
IEnumerable<XElement> elem_list = doc.Descendants("qa");
枫林﹌晚霞¤ 2024-09-04 23:54:53

我也许会将您的查询更改为看起来更像这样的内容

var query = from q_a in document.Descendants("q_a")
            select new
            {
                Number = (int)q_a.Element("q_a_num"),
                Question = (string)q_a.Element("q_"),
                Answer = (string)q_a.Element("_a")
            };

这样,您将从每个 q_a 后代中将内部元素提取到 IEnumerable<[Anonymous Type]>< /code>,每个对象包含数字、问题和答案。

但是,如果您只想提取名称为 q_a 的 XElement,则可以使用 where 子句来执行此操作。

IEnumerable<XElement> elem_list = elems.Where(elem => elem.Name.LocalName == "q_a");

当然,正如 David B 所示,这里不需要 where 子句。

IEnumerable<XElement> elem_list = elems.Elements("q_a");

I would perhaps change your query to something that looks more like this

var query = from q_a in document.Descendants("q_a")
            select new
            {
                Number = (int)q_a.Element("q_a_num"),
                Question = (string)q_a.Element("q_"),
                Answer = (string)q_a.Element("_a")
            };

With this, you'll pull from each of your q_a descendants the inner elements into an IEnumerable<[Anonymous Type]>, each object containing the number, question, and answer.

However, if you just want to extract the XElements where the name is q_a, you could do this using a where clause.

IEnumerable<XElement> elem_list = elems.Where(elem => elem.Name.LocalName == "q_a");

Of course, as David B showed, the where clause is not necessary here.

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