linq根据嵌套孩子的属性选择父

发布于 2025-02-12 03:58:56 字数 1262 浏览 2 评论 0原文

我正在使用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”属性,它将返回属性的名称和值:

​很复杂,原因有几个。

  1. XML树的深度(嵌套儿童)。
  2. 需要使用属性值。

让我知道是否有人知道如何执行这种类型的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:

enter image description here

I think this query is complex for a couple reasons.

  1. Depth of the XML Tree (nested children).
  2. The need to use the attribute values.

Let me know if anyone knows how to do this type of Linq query.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

攒一口袋星星 2025-02-19 03:58:56
var referenceIds = xElement.Element("PropertySetBindings")
    .Elements("PropertySetBind")
    .Where(x => x.Elements("Rules")
        .Any(r => r.Elements("Include")
            .Any(i => i.Attributes("entityType")
                .Any(a => a.Value == "IfcBeam")
            )
        )
    )
    .Select(x => x.Attribute("referenceId"))
    .Where(x => x != null)
    .Select(x => x.Value);

它的工作如下:

  1. 选择propertySetBindings元素
  2. 选择propertysetBind儿童
  3. 将儿童过滤给具有规则元素的儿童,其中包括/code>具有entityType属性的元素,其值为“ ifcbeam”。
  4. 从这些propertySetBind元素中,选择“参考”属性
  5. 检查为null(存在属性)的'reptimID'属性(属性)
  6. 选择属性的值(因此您没有“ referentID = value”,只是值)
var referenceIds = xElement.Element("PropertySetBindings")
    .Elements("PropertySetBind")
    .Where(x => x.Elements("Rules")
        .Any(r => r.Elements("Include")
            .Any(i => i.Attributes("entityType")
                .Any(a => a.Value == "IfcBeam")
            )
        )
    )
    .Select(x => x.Attribute("referenceId"))
    .Where(x => x != null)
    .Select(x => x.Value);

It works as follows:

  1. select the PropertySetBindings element
  2. select the PropertySetBind children
  3. filter the children to children with Rules elements, that have Include elements that have entityType attributes, that have a value of 'IfcBeam'.
  4. from those PropertySetBind elements, select the 'referenceId' attribute
  5. check for null (attribute exists)
  6. select the value of the attribute (so you dont have the "referenceId=value", just the value)
傲鸠 2025-02-19 03:58:56

好吧,我找到了一个工作解决方案!
我只能让它使用此查询符号(我认为称为)而不是lambda符号。这允许访问属性的值。

            var bindings = from binding in xElement.Elements("PropertySetBindings")
                       from bind in binding.Elements("PropertySetBind")
                       from ru in bind.Elements("Rules")
                       from inc in ru.Elements("Include")
                       where (string)inc.Attribute("entityType") == "IfcBeam"
                       select bind.Attribute("referenceId").Value;

让我知道您是否对此问题有更优雅的解决方案。

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.

            var bindings = from binding in xElement.Elements("PropertySetBindings")
                       from bind in binding.Elements("PropertySetBind")
                       from ru in bind.Elements("Rules")
                       from inc in ru.Elements("Include")
                       where (string)inc.Attribute("entityType") == "IfcBeam"
                       select bind.Attribute("referenceId").Value;

Let me know if you have a more elegant solution to this problem.

淡淡绿茶香 2025-02-19 03:58:56

找到了此解决方案:

 var bindings = xElement.Elements("PropertySetBindings")
    .Elements("PropertySetBind")                       
    .Where(x => x.Elements("Rules").FirstOrDefault()
    .Elements("Include").FirstOrDefault()                         
    .Attributes("entityType").FirstOrDefault().Value.Equals("IfcBeam"))
    .Select(x => x.Attributes("referenceId").FirstOrDefault().Value);  

Found this solution:

 var bindings = xElement.Elements("PropertySetBindings")
    .Elements("PropertySetBind")                       
    .Where(x => x.Elements("Rules").FirstOrDefault()
    .Elements("Include").FirstOrDefault()                         
    .Attributes("entityType").FirstOrDefault().Value.Equals("IfcBeam"))
    .Select(x => x.Attributes("referenceId").FirstOrDefault().Value);  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文