Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#linq通过选择child.child.attribute来选择元素_C#_Xml_Linq_Where - Fatal编程技术网

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

c#linq通过选择child.child.attribute来选择元素,c#,xml,linq,where,C#,Xml,Linq,Where,我想选择一个标签。当包含元素时,应进行选择 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.

我想选择一个标签。当包含元素时,应进行选择

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>
...
但是在第二个例子中,我得到了一个NullReferenceException。少了什么?这是我与linq和c#的第一次尝试,所以我可能正在考虑复杂化。我认为问题出现了,因为第一个标记没有child的。这就是为什么我将选择分为“Where”。第一个标签应仅选择带有子标签的标签,第二个标签应与最后一个标签完全匹配。到目前为止,我的意图是

有人能给我一个提示吗

非常感谢您的想法和帮助

有关信息:

我使用以下名称空间

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

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

通过坚持使用
IEnumerable
而不是“退出monad”并使用显式转换,您可以避免执行空检查,例如:

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

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

通过坚持使用
IEnumerable
而不是“退出monad”并使用显式转换,您可以避免执行空检查,例如:

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

这是我们想要的。令人惊讶的是,不需要“Where”来选择一个元素。这可以根据需要工作。令人惊讶的是,只选择一个元素不需要“Where”。