c# linq通过选择child.child.attribute来选择元素

本文关键字:child 选择 元素 attribute linq | 更新日期: 2023-09-27 18:07:23

我想选择one标签。选择应做,当有元素。

xml文件看起来像

    <?xml version="1.0" encoding="utf-8" standalone="no"?>
    <graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:java="http://www.yworks.com/xml/yfiles-common/1.0/java" xmlns:sys="http://www.yworks.com/xml/yfiles-common/markup/primitives/2.0" xmlns:x="http://www.yworks.com/xml/yfiles-common/markup/2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:y="http://www.yworks.com/xml/graphml" xmlns:yed="http://www.yworks.com/xml/yed/3" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://www.yworks.com/xml/schema/graphml/1.1/ygraphml.xsd">
      <!--Created by yEd 3.14.2-->
      <key attr.name="description" attr.type="string" for="graph" id="d0" />
      <key for="port" id="d1" yfiles.type="portgraphics" />
      <key for="port" id="d2" yfiles.type="portgeometry" />
      <key for="port" id="d3" yfiles.type="portuserdata" />
      <key attr.name="url" attr.type="string" for="node" id="d4" />
      <key attr.name="description" attr.type="string" for="node" id="d5" />
      <key for="node" id="d6" yfiles.type="nodegraphics" />
      <key for="graphml" id="d7" yfiles.type="resources" />
      <key for="edge" id="d8" yfiles.type="portconstraints" />
      <key attr.name="url" attr.type="string" for="edge" id="d9" />
      <key attr.name="description" attr.type="string" for="edge" id="d10" />
      <key for="edge" id="d11" yfiles.type="edgegraphics" />
      <graph edgedefault="directed" id="G">
        <data key="d0" />
        <node id="n0" yfiles.foldertype="group">
          <data key="d6">
            <y:TableNode configuration="YED_TABLE_NODE">
              <y:Geometry height="646.8359897640048" width="1012.9110704527719" x="0.0" y="-646.8359897640048" />
              <y:Fill color="#ECF5FF" color2="#0042F440" transparent="false" />
              <y:BorderStyle color="#000000" type="line" width="1.0" />
              <y:NodeLabel alignment="center" autoSizePolicy="content" fontFamily="Dialog" fontSize="15" fontStyle="plain" hasBackgroundColor="false" hasLineColor="false" height="22.37646484375" modelName="internal" modelPosition="t" textColor="#000000" visible="true" width="230.7578125" x="391.07662897638596" y="4.0">title</y:NodeLabel>
              <y:NodeLabel alignment="center" autoSizePolicy="content" fontFamily="Dialog" fontSize="12" fontStyle="plain" hasBackgroundColor="false" hasLineColor="false" height="18.701171875" modelName="custom" textColor="#000000" visible="true" width="54.712890625" x="111.14444510514247" y="33.0">col0<y:LabelModel><y:ColumnNodeLabelModel offset="3.0" /></y:LabelModel><y:ModelParameter><y:ColumnNodeLabelModelParameter id="column_0" inside="true" verticalPosition="0.0" /></y:ModelParameter></y:NodeLabel>
              <y:NodeLabel alignment="center" autoSizePolicy="content" fontFamily="Dialog" fontSize="12" fontStyle="plain" hasBackgroundColor="false" hasLineColor="false" height="18.701171875" modelName="custom" textColor="#000000" visible="true" width="72.712890625" x="482.3174666703262" y="33.0">col1<y:LabelModel><y:ColumnNodeLabelModel offset="3.0" /></y:LabelModel><y:ModelParameter><y:ColumnNodeLabelModelParameter id="column_1" inside="true" verticalPosition="0.0" /></y:ModelParameter></y:NodeLabel>
              <y:NodeLabel alignment="center" autoSizePolicy="content" fontFamily="Dialog" fontSize="12" fontStyle="plain" hasBackgroundColor="false" hasLineColor="false" height="18.701171875" modelName="custom" textColor="#000000" visible="true" width="58.046875" x="869.6051192915697" y="33.0">col2<y:LabelModel><y:ColumnNodeLabelModel offset="3.0" /></y:LabelModel><y:ModelParameter><y:ColumnNodeLabelModelParameter id="column_2" inside="true" verticalPosition="0.0" /></y:ModelParameter></y:NodeLabel>
              <y:NodeLabel alignment="center" autoSizePolicy="content" fontFamily="Dialog" fontSize="12" fontStyle="plain" hasBackgroundColor="false" hasLineColor="false" height="18.701171875" modelName="custom" rotationAngle="270.0" textColor="#000000" visible="true" width="40.01171875" x="3.0" y="330.4121355070024">row0<y:LabelModel><y:RowNodeLabelModel offset="3.0" /></y:LabelModel><y:ModelParameter><y:RowNodeLabelModelParameter horizontalPosition="0.0" id="row_0" inside="true" /></y:ModelParameter></y:NodeLabel>
              <y:StyleProperties>
...

我只想选择带有后代("y:RowNodeLabelModelParameter").attribute("id")的标签。Value = "row_0"

So far So easy.

我尝试了下面的代码

    XElement TableNode = yedFile.Descendants(y + "TableNode").FirstOrDefault();
    XElement NodeLblRow = new XElement(TableNode.Elements(y + "NodeLabel")
                                                .Where(t => t.HasElements )
                                                .Where(x => x.Element(y + "ModelParameter")
                                                             .Element(y + "RowNodeLabelModelParameter")
                                                             .Attribute("id").Value == "row_0"
                                                      ).FirstOrDefault()
                                       );

,但在第二,我得到一个NullReferenceException。缺少了什么?这是我第一次尝试使用linq和c#,所以可能是我想得太复杂了。我认为出现问题是因为第一个标签没有子标签。这就是为什么我把选择分为"Where"。第一个应该只选择 Tag's with child's and第二个应该完全匹配最后一个标签。到目前为止我的意图。

谁能给我点提示吗?

非常感谢您的建议和帮助。

信息:

我使用以下命名空间

XNamespace   xmlns        = XNamespace.Get("http://graphml.graphdrawing.org/xmlns");
XNamespace /*xmlns:*/java = XNamespace.Get("http://www.yworks.com/xml/yfiles-common/1.0/java");
XNamespace /*xmlns:*/sys  = XNamespace.Get("http://www.yworks.com/xml/yfiles-common/markup/primitives/2.0");
XNamespace /*xmlns:*/x    = XNamespace.Get("http://www.yworks.com/xml/yfiles-common/markup/2.0");
XNamespace /*xmlns:*/xsi  = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
XNamespace /*xmlns:*/y    = XNamespace.Get("http://www.yworks.com/xml/graphml");
XNamespace /*xmlns:*/yed  = XNamespace.Get("http://www.yworks.com/xml/yed/3");

c# linq通过选择child.child.attribute来选择元素

当你遇到一个元素没有组成你的查询的元素或属性时,你会得到一个空引用异常

通过使用IEnumerable而不是'退出monad'并使用显式转换,您可以避免必须进行空检查,例如:

var element = yedFile.Descendants(y + "NodeLabel")
    .FirstOrDefault(e => (string)e.Elements(y + "ModelParameter")
        .Elements(y + "RowNodeLabelModelParameter")
        .Attributes("id")
        .SingleOrDefault() == "row_0");