c#通过选择child.child.attribute选择Element

时间:2015-08-12 09:35:46

标签: c# xml linq where

我想选择一个< NodeLabel>标签。当<时,应该进行选择。 NodeLabel>有元素。

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>
...

我只想选择&lt; NodeLabel&gt;标记后代(“y:RowNodeLabelModelParameter”)。属性(“id”)。value =“row_0”

到目前为止很容易。

我尝试使用以下代码

    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#的第一次尝试,所以可能是我想要复杂化。我认为问题出现了,因为第一个&lt; NodeLabel&gt;标签没有孩子的。这就是我将选择分为“Where”的原因。第一个应该只选择&lt; NodeLabel&gt;带有孩子的标签和第二个应该完全匹配最后一个&lt; NodeLabel&gt;标签。到目前为止我的意图。

有人能给我一个暗示吗?

非常感谢您的想法和帮助。

有关信息:

我使用以下命名空间

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");

1 个答案:

答案 0 :(得分:0)

当您遇到没有构成查询的元素或属性的元素时,您将获得空引用异常。

通过坚持使用print_r(json_decode($Str, true)); 而不是“退出monad”并使用显式转换,您可以避免进行空检查,例如:

IEnumerable