我有一个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);
}
}
我还尝试过更改查询以列出它仍然无法使用,有什么建议吗?
答案 0 :(得分:3)
用于选择的LINQ查询不正确。使用它来提取其中具有“错误”值的节点
var query1 = from q in document.Descendants("BA_Part_I").Descendants("Q")
where q.Value == "Wrong"
select q;`
请注意,我们要在BA_Part_I节点中拾取Descendants(“ Q”),然后检查该节点的值。