按后代元素的属性过滤 XElement
我试图根据该元素的子元素的属性来选择元素。
原始 Xml:
<Root>
<Element1 id="1">
<element2>XXXX</element2>
<element2>XXXX</element2>
<element2>XXXX</element2>
<Filter attr1="1" attr2="0" attr3="0" attr4="1" attr5="0"></Filter>
</Element1>
<Element1 id="2">
<element2>XXXX</element2>
<element2>XXXX</element2>
<element2>XXXX</element2>
<Filter attr1="1" attr2="0" attr3="0" attr4="1" attr5="0"></Filter>
</Element1>
<Element1 id="3">
<element2>XXXX</element2>
<element2>XXXX</element2>
<element2>XXXX</element2>
<Filter attr1="1" attr2="0" attr3="0" attr4="1" attr5="0"></Filter>
</Element1>
</Root>
这是我到目前为止所做的:
Dim xmlElement = (From rec In RecipeXmlDocument.Descendants("RECEPT") _
Where (rec.Descendants("Filter").@poultry = Ing.Poultry.ToString() _
Or rec.Descendants("Filter").@attr1 = "1" _
Or rec.Descendants("Filter").@attr2 = "0" _
And (rec.Descendants("Filter").@attr3 = "1" _
Or rec.Descendants("Filter").@attr4 = "0" _
Or rec.Descendants("Filter").@attr5 = "1"
这会引发以下错误:“引发了‘System.Linq.SystemCore_EnumerableDebugViewEmptyException’类型的异常。”
代码试图做的是选择所有 Element1,其中该 element1 的过滤器元素与语句的 where 子句匹配。
我对 linq 相当陌生,我不太确定我做错了什么。
I'm trying to select elements based on the attributes of sub a element of that element.
Original Xml:
<Root>
<Element1 id="1">
<element2>XXXX</element2>
<element2>XXXX</element2>
<element2>XXXX</element2>
<Filter attr1="1" attr2="0" attr3="0" attr4="1" attr5="0"></Filter>
</Element1>
<Element1 id="2">
<element2>XXXX</element2>
<element2>XXXX</element2>
<element2>XXXX</element2>
<Filter attr1="1" attr2="0" attr3="0" attr4="1" attr5="0"></Filter>
</Element1>
<Element1 id="3">
<element2>XXXX</element2>
<element2>XXXX</element2>
<element2>XXXX</element2>
<Filter attr1="1" attr2="0" attr3="0" attr4="1" attr5="0"></Filter>
</Element1>
</Root>
This is what I've done so far:
Dim xmlElement = (From rec In RecipeXmlDocument.Descendants("RECEPT") _
Where (rec.Descendants("Filter").@poultry = Ing.Poultry.ToString() _
Or rec.Descendants("Filter").@attr1 = "1" _
Or rec.Descendants("Filter").@attr2 = "0" _
And (rec.Descendants("Filter").@attr3 = "1" _
Or rec.Descendants("Filter").@attr4 = "0" _
Or rec.Descendants("Filter").@attr5 = "1"
This throws the following error: "Exception of type 'System.Linq.SystemCore_EnumerableDebugViewEmptyException' was thrown."
What the code is trying to do is to select all the Element1s where the filter element of that element1 matches the where clause of the statement.
I'm fairly new to linq and i'm not exactly sure what I'm doing wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您到底想从 xml 中选择什么?您的 linq 语句中缺少 select 语句。
在 linq 语句的末尾添加 Select,例如 Select(无论标签中的什么)
此外,您正在“越过”您要搜索的元素,即
应该是
下面是一个修改后的语句,您需要将您的select 语句
注意:您必须在标签内添加一些元素才能选择它们
What exactly do you want to select from the xml? The select statement is missing from your linq statement.
Add the Select at the end of your linq statement, e.g Select (whatever from the tag)
Additionally, you are navigating "over" the element you are trying to search, ie
should be
Below is an amended statement to which you need to concatenate your select statement
NOTE:You will have to add some elements inside the tag to be able to select them