Noob LINQ - 使用 XDocument 读取、过滤 XML
我刚刚学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是 Name 属性不是字符串,而是 XName.当你ToString它时,你得到的东西比你想象的要多得多。
虽然可以按照您尝试的方式编写查询,但也要考虑以下可能性:
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:
我也许会将您的查询更改为看起来更像这样的内容
这样,您将从每个
q_a
后代中将内部元素提取到IEnumerable<[Anonymous Type]>< /code>,每个对象包含数字、问题和答案。
但是,如果您只想提取名称为 q_a 的 XElement,则可以使用 where 子句来执行此操作。
当然,正如 David B 所示,这里不需要 where 子句。
I would perhaps change your query to something that looks more like this
With this, you'll pull from each of your
q_a
descendants the inner elements into anIEnumerable<[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.
Of course, as David B showed, the where clause is not necessary here.