linq根据嵌套孩子的属性选择父
我正在使用system.xml.linq,以便从此XML文件中获取所需的信息。我需要最终得到一个参考列的列表,这些列表在子元素中具有正确的实体类型。
这是我正在使用的XML文件的示例。
<PropertySetBindings>
<PropertySetBind referenceId="assemblies">
<Rules>
<Include entityType="IfcElementAssembly" subtypes="true" />
</Rules>
</PropertySetBind>
<PropertySetBind referenceId="beam_common">
<Rules>
<Include entityType="IfcBeam" subtypes="false" />
</Rules>
</PropertySetBind>
<PropertySetBind referenceId="column_common">
<Rules>
<Include entityType="IfcColumn" subtypes="false" />
</Rules>
</PropertySetBind>
这是我可以提出的最好的LINQ查询,但没有返回任何内容。一旦我尝试查询属性,
var bindings = xElement.Elements("PropertySetBindings")
.Elements("PropertySetBind")
.Where(x => x.Elements("Rules")
.Elements("Include")
.Attributes("entityType").FirstOrDefault().Equals("IfcBeam"))
.Select(x => x.Attribute("referenceId"));
我认为这可能与访问属性的价值有关,似乎没有什么可用。属性没有属性(“ entityType”)。值也,如果我尝试简单地返回所有“ entityType”属性,它将返回属性的名称和值:
很复杂,原因有几个。
- XML树的深度(嵌套儿童)。
- 需要使用属性值。
让我知道是否有人知道如何执行这种类型的LINQ查询。
I am using System.Xml.Linq in order to get the information I need from this XML file. I need to end up with a List of the referenceId's that have the correct entityType in the child element.
Here is a sample of the XML file I am working with.
<PropertySetBindings>
<PropertySetBind referenceId="assemblies">
<Rules>
<Include entityType="IfcElementAssembly" subtypes="true" />
</Rules>
</PropertySetBind>
<PropertySetBind referenceId="beam_common">
<Rules>
<Include entityType="IfcBeam" subtypes="false" />
</Rules>
</PropertySetBind>
<PropertySetBind referenceId="column_common">
<Rules>
<Include entityType="IfcColumn" subtypes="false" />
</Rules>
</PropertySetBind>
This is the best Linq query I can come up with, but it doesn't return anything. Nothing seems to work as soon as I try to query the attributes
var bindings = xElement.Elements("PropertySetBindings")
.Elements("PropertySetBind")
.Where(x => x.Elements("Rules")
.Elements("Include")
.Attributes("entityType").FirstOrDefault().Equals("IfcBeam"))
.Select(x => x.Attribute("referenceId"));
I think this might have to do with accessing the value of the attribute. There is no property for Attributes("entityType").Value Also, if I try to simply return all the "entityType" attributes, it returns the name and value of the attribute:
I think this query is complex for a couple reasons.
- Depth of the XML Tree (nested children).
- The need to use the attribute values.
Let me know if anyone knows how to do this type of Linq query.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它的工作如下:
propertySetBindings
元素propertysetBind
儿童规则
元素的儿童,其中包括
/code>具有
entityType
属性的元素,其值为“ ifcbeam”。propertySetBind
元素中,选择“参考”属性It works as follows:
PropertySetBindings
elementPropertySetBind
childrenRules
elements, that haveInclude
elements that haveentityType
attributes, that have a value of 'IfcBeam'.PropertySetBind
elements, select the 'referenceId' attribute好吧,我找到了一个工作解决方案!
我只能让它使用此查询符号(我认为称为)而不是lambda符号。这允许访问属性的值。
让我知道您是否对此问题有更优雅的解决方案。
Okay, I found a working solution!
I can only get it to work with using this query notation (I think its called) instead of the Lambda notation. This allows for accessing the value of the attribute.
Let me know if you have a more elegant solution to this problem.
找到了此解决方案:
Found this solution: