使用 linq 从 xml 获取特定节点
我在对 xml 树进行 linq 查询时遇到了一些问题。树结构如下所示:
<Student>
<ID> Hello </Hello>
<Classroom>
<Name> 1B </Name>
<Year> 1 </Year>
</Classroom>
<Classroom>
<Name> 2B </Name>
<Year> 2 </Year>
</Classroom>
<Classroom>
<Name> 3B </Name>
<Year> 3 </Year>
</Classroom>
</Student>
现在,这是 5 个学生条目中的一个。假设我从另一个方法传递了一个 XElement Student 节点,并且我想在给定 XElement Student 节点和教室名称的情况下搜索教室。所以我必须写一个这样的方法:
getClassRoomNode(XElement StudentNode, string classroomName)
这就是我尝试过的。请让我知道我错在哪里
XElement classroom = StudentNode.Descendants("Classroom")
.Where(arg => arg.Element("Name").Value == classroomName)
.Select(arg => arg.Parent)
.First();
这会再次返回 StudentNode 而不是教室节点。有人可以帮我解决这个问题吗?
Im having some trouble with a linq query into an xml tree. Here is what the tree structure looks like:
<Student>
<ID> Hello </Hello>
<Classroom>
<Name> 1B </Name>
<Year> 1 </Year>
</Classroom>
<Classroom>
<Name> 2B </Name>
<Year> 2 </Year>
</Classroom>
<Classroom>
<Name> 3B </Name>
<Year> 3 </Year>
</Classroom>
</Student>
Now this is one student entry among 5. say i am passed an XElement Student node from another method and i want to search for a classroom given the XElement Student node and the classroom name. So i have to write a method like this:
getClassRoomNode(XElement StudentNode, string classroomName)
This is what ive tried. Please let me know where i am wrong
XElement classroom = StudentNode.Descendants("Classroom")
.Where(arg => arg.Element("Name").Value == classroomName)
.Select(arg => arg.Parent)
.First();
This returns the StudentNode back again instead of a classroom node. Can anyone please help me out with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是因为您正在选择教室节点的父节点。只需摆脱最后一个选择器即可:
或者更简单,正如指出的那样,将 First 和Where 组合在一起。
然而,如果没有匹配,这将抛出异常,另一方面,FirstOrDefault 将在不匹配时返回 null;
This is because you are selecting the parent of the classroom nodes. Just get rid of the last selector:
Or even simpler, as was pointed out combine First and Where together.
This will however throw an exception if there is no match, FirstOrDefault on the otherhand would return null on no match;
您不需要选择父级,只需获取
First()
,甚至更好的Single()
班级房间。我建议最好是犯错,而不是忽略后续的匹配。如果目的是匹配许多比赛中的第一场比赛,那么显然,
You don't need to select parent, just the get the
First()
, or even betterSingle()
class room.I suggest it is better to error rather than ignore subsequent matches. If the intention is to match the first of many matches then obviously,
摆脱选择,您正在重新选择父级!
Get rid of the Select, you are reselecting the parent!
您的代码完全按照其指示执行:选择; name 元素的内容等于 classroomName 的节点,然后选择该的父级。节点,即节点。
修复:
这将返回第一个name 元素的内容等于classroomName 的节点。
Your code does exactly what it's told to: select the <classrom> node where the content of the name element equals classroomName, and then select the parent of that <classrom> node, i.e. the <student> node.
Fix:
This returns the first <classrom> node where the content of the name element equals classroomName.
您正在选择“课堂”节点的父节点。像这样更改查询:
您不应使用 First,而应使用 FirstOrDefault 并检查 null 值,以阻止在根据查询未找到 Classroom 节点时可能发生的任何异常。 (如果您在代码中的其他位置处理此异常,请忽略此内容)
Enumerable.FirstOrDefault
You are selecting the parent node of the Classroom node. Change the query like so:
Instead of using First you should use FirstOrDefault and check for a null value to stop any exceptions which may occur if no Classroom nodes are found based on your query. (Ignore this if you are handling this exception elsewhere in your code)
Enumerable.FirstOrDefault