【问题标题】:How to fix query to get wrong answers如何修复查询以获得错误的答案
【发布时间】:2019-02-19 04:46:16
【问题描述】:

我有一个 XML 文件,我需要过滤以获得“错误”的答案。我的查询仅在第一个答案的值错误时执行。它不会过滤结果。

<?xml version="1.0" encoding="utf-8" ?>
<Test>
  <BA_Part_I>
    <Q _001="A"></Q>
    <Q _002="C"></Q>
    <Q _003="B, D"></Q>
    <Q _004="2, 4, 9"></Q>
    <Q _005="A"></Q>
    <Q _006="2, 6, 7, 4, 8"></Q>
    <Q _007="A"></Q>
    <Q _008="C"></Q>
    <Q _009="A">Wrong</Q>
    <Q _010="D"></Q>
    <Q _011="C"></Q>
    <Q _012="1, 3"></Q>
    <Q _013="B, D"></Q>
    <Q _014="B"></Q>
    <Q _015="B"></Q>
    <Q _016="A, F">Wrong</Q>
    <Q _017="A"></Q>
    <Q _018="D"></Q>
    <Q _019="3, 1, 1, 2"></Q>
    <Q _020="B"></Q>
    <Q _021="A, B"></Q>
  </BA_Part_I>
</Test>

using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;

//...
    static void Main(string[] args)
    {
        XDocument document = XDocument.Parse(File.ReadAllText("Tracker.xml"));

        var query1 = from q in document.Descendants("BA_Part_I")
                        where q.Element("Q").Value == "Wrong"
                        select q;

        foreach (var item in query1)
        {
            Console.WriteLine(item);
        }
    }

我也尝试更改查询以列出它仍然不起作用,有什么建议吗?

【问题讨论】:

    标签: c# linq-to-xml


    【解决方案1】:

    用于选择的 LINQ 查询不正确。使用它来提取其中具有“错误”值的节点

    var query1 = from q in document.Descendants("BA_Part_I").Descendants("Q")
                         where q.Value == "Wrong"
                         select q;`
    

    请注意,我们在 BA_Part_I 节点中选取 Descendants("Q"),然后检查该节点的值。

    【讨论】:

      猜你喜欢
      • 2014-11-19
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 2021-05-04
      • 2019-10-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多