Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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# XmlReader值不通过If语句,但它应该_C#_Xml_If Statement_Xml Parsing_Graphml - Fatal编程技术网

C# XmlReader值不通过If语句,但它应该

C# XmlReader值不通过If语句,但它应该,c#,xml,if-statement,xml-parsing,graphml,C#,Xml,If Statement,Xml Parsing,Graphml,我编写了一个.graphml解析器,在某一点上,我需要从特定的标记中读取属性值。Graphml文件如下所示: <?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="htt

我编写了一个.graphml解析器,在某一点上,我需要从特定的标记中读取属性值。Graphml文件如下所示:

<?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.19.1-->
  <key for="port" id="d0" yfiles.type="portgraphics"/>
  <key for="port" id="d1" yfiles.type="portgeometry"/>
  <key for="port" id="d2" yfiles.type="portuserdata"/>
  <key attr.name="url" attr.type="string" for="node" id="d3"/>
  <key attr.name="description" attr.type="string" for="node" id="d4"/>
  <key for="node" id="d5" yfiles.type="nodegraphics"/>
  <key for="graphml" id="d6" yfiles.type="resources"/>
  <key attr.name="url" attr.type="string" for="edge" id="d7"/>
  <key attr.name="description" attr.type="string" for="edge" id="d8"/>
  <key for="edge" id="d9" yfiles.type="edgegraphics"/>
  <graph edgedefault="directed" id="G">
    <node id="n0">
      <data key="d5">
        <y:UMLClassNode>
          <y:Geometry height="116.0" width="131.0" x="1301.3333333333333" y="41.0"/>
          <y:Fill color="#FFCC00" transparent="false"/>
          <y:BorderStyle color="#000000" type="line" width="1.0"/>
          <y:NodeLabel alignment="center" autoSizePolicy="content" fontFamily="Dialog" fontSize="12" fontStyle="bold" hasBackgroundColor="false" hasLineColor="false" height="18.701171875" horizontalTextPosition="center" iconTextGap="4" modelName="internal" modelPosition="c" textColor="#000000" verticalTextPosition="bottom" visible="true" width="138.7421875" x="-3.87109375" xml:space="preserve" y="3.0">&lt;&lt;interface&gt;&gt;Employee</y:NodeLabel>
          <y:UML clipContent="true" constraint="" hasDetailsColor="false" omitDetails="false" stereotype="" use3DEffect="true">
            <y:AttributeLabel xml:space="preserve">+name:string
+age:int</y:AttributeLabel>
            <y:MethodLabel xml:space="preserve">getName(value:string):String
getTitle():String
getStaffNo():Number
getRoom():String
getPhone()</y:MethodLabel>
          </y:UML>
        </y:UMLClassNode>
      </data>
    </node>
    <node id="n1">
      <data key="d3" xml:space="preserve"/>
      <data key="d4"/>
      <data key="d5">
        <y:UMLClassNode>
          <y:Geometry height="116.0" width="206.0" x="1422.0" y="197.0"/>
          <y:Fill color="#FFCC00" transparent="false"/>
          <y:BorderStyle color="#000000" type="line" width="1.0"/>
          <y:NodeLabel alignment="center" autoSizePolicy="content" fontFamily="Dialog" fontSize="13" fontStyle="bold" hasBackgroundColor="false" hasLineColor="false" height="19.92626953125" horizontalTextPosition="center" iconTextGap="4" modelName="custom" textColor="#000000" verticalTextPosition="bottom" visible="true" width="64.67724609375" x="70.661376953125" xml:space="preserve" y="3.0">Customer<y:LabelModel><y:SmartNodeLabelModel distance="4.0"/></y:LabelModel><y:ModelParameter><y:SmartNodeLabelModelParameter labelRatioX="0.0" labelRatioY="0.0" nodeRatioX="0.0" nodeRatioY="0.0" offsetX="0.0" offsetY="0.0" upX="0.0" upY="-1.0"/></y:ModelParameter></y:NodeLabel>
          <y:UML clipContent="true" constraint="" hasDetailsColor="false" omitDetails="false" stereotype="" use3DEffect="true">
            <y:AttributeLabel xml:space="preserve">+name:string</y:AttributeLabel>
            <y:MethodLabel xml:space="preserve">+getOrder():string
+payOrder(order:string):int</y:MethodLabel>
          </y:UML>
        </y:UMLClassNode>
      </data>
    </node>
    <node id="n2">
      <data key="d3" xml:space="preserve"/>
      <data key="d4"/>
      <data key="d5">
        <y:UMLClassNode>
          <y:Geometry height="86.0" width="100.0" x="1086.0" y="197.0"/>
          <y:Fill color="#FFCC00" transparent="false"/>
          <y:BorderStyle color="#000000" type="line" width="1.0"/>
          <y:NodeLabel alignment="center" autoSizePolicy="content" fontFamily="Dialog" fontSize="13" fontStyle="bold" hasBackgroundColor="false" hasLineColor="false" height="19.92626953125" horizontalTextPosition="center" iconTextGap="4" modelName="custom" textColor="#000000" verticalTextPosition="bottom" visible="true" width="32.9072265625" x="33.54638671875" xml:space="preserve" y="3.0">User<y:LabelModel><y:SmartNodeLabelModel distance="4.0"/></y:LabelModel><y:ModelParameter><y:SmartNodeLabelModelParameter labelRatioX="0.0" labelRatioY="0.0" nodeRatioX="0.0" nodeRatioY="0.0" offsetX="0.0" offsetY="0.0" upX="0.0" upY="-1.0"/></y:ModelParameter></y:NodeLabel>
          <y:UML clipContent="true" constraint="" hasDetailsColor="false" omitDetails="false" stereotype="" use3DEffect="true">
            <y:AttributeLabel xml:space="preserve">+name:string</y:AttributeLabel>
            <y:MethodLabel xml:space="preserve">getName()</y:MethodLabel>
          </y:UML>
        </y:UMLClassNode>
      </data>
    </node>
  </graph>
  <data key="d6">
    <y:Resources/>
  </data>
</graphml>


public List<string> getNodeAttributeValue(XmlReader reader)
        {
            List<string> id = new List<string>();

            while (reader.Read())
            {
                reader.MoveToAttribute("id");
                while (reader.ReadAttributeValue())
                {
                    if (xmlReader.Value.Contains('n'))
                    {
                        string idElement = xmlReader.Value;
                        id.Add(idElement);
                        return id;
                    }
                }
            }
            return id;
        }

我需要的特定标签是 现在我需要id属性的信息

因此,本节的解析器代码如下所示:

<?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.19.1-->
  <key for="port" id="d0" yfiles.type="portgraphics"/>
  <key for="port" id="d1" yfiles.type="portgeometry"/>
  <key for="port" id="d2" yfiles.type="portuserdata"/>
  <key attr.name="url" attr.type="string" for="node" id="d3"/>
  <key attr.name="description" attr.type="string" for="node" id="d4"/>
  <key for="node" id="d5" yfiles.type="nodegraphics"/>
  <key for="graphml" id="d6" yfiles.type="resources"/>
  <key attr.name="url" attr.type="string" for="edge" id="d7"/>
  <key attr.name="description" attr.type="string" for="edge" id="d8"/>
  <key for="edge" id="d9" yfiles.type="edgegraphics"/>
  <graph edgedefault="directed" id="G">
    <node id="n0">
      <data key="d5">
        <y:UMLClassNode>
          <y:Geometry height="116.0" width="131.0" x="1301.3333333333333" y="41.0"/>
          <y:Fill color="#FFCC00" transparent="false"/>
          <y:BorderStyle color="#000000" type="line" width="1.0"/>
          <y:NodeLabel alignment="center" autoSizePolicy="content" fontFamily="Dialog" fontSize="12" fontStyle="bold" hasBackgroundColor="false" hasLineColor="false" height="18.701171875" horizontalTextPosition="center" iconTextGap="4" modelName="internal" modelPosition="c" textColor="#000000" verticalTextPosition="bottom" visible="true" width="138.7421875" x="-3.87109375" xml:space="preserve" y="3.0">&lt;&lt;interface&gt;&gt;Employee</y:NodeLabel>
          <y:UML clipContent="true" constraint="" hasDetailsColor="false" omitDetails="false" stereotype="" use3DEffect="true">
            <y:AttributeLabel xml:space="preserve">+name:string
+age:int</y:AttributeLabel>
            <y:MethodLabel xml:space="preserve">getName(value:string):String
getTitle():String
getStaffNo():Number
getRoom():String
getPhone()</y:MethodLabel>
          </y:UML>
        </y:UMLClassNode>
      </data>
    </node>
    <node id="n1">
      <data key="d3" xml:space="preserve"/>
      <data key="d4"/>
      <data key="d5">
        <y:UMLClassNode>
          <y:Geometry height="116.0" width="206.0" x="1422.0" y="197.0"/>
          <y:Fill color="#FFCC00" transparent="false"/>
          <y:BorderStyle color="#000000" type="line" width="1.0"/>
          <y:NodeLabel alignment="center" autoSizePolicy="content" fontFamily="Dialog" fontSize="13" fontStyle="bold" hasBackgroundColor="false" hasLineColor="false" height="19.92626953125" horizontalTextPosition="center" iconTextGap="4" modelName="custom" textColor="#000000" verticalTextPosition="bottom" visible="true" width="64.67724609375" x="70.661376953125" xml:space="preserve" y="3.0">Customer<y:LabelModel><y:SmartNodeLabelModel distance="4.0"/></y:LabelModel><y:ModelParameter><y:SmartNodeLabelModelParameter labelRatioX="0.0" labelRatioY="0.0" nodeRatioX="0.0" nodeRatioY="0.0" offsetX="0.0" offsetY="0.0" upX="0.0" upY="-1.0"/></y:ModelParameter></y:NodeLabel>
          <y:UML clipContent="true" constraint="" hasDetailsColor="false" omitDetails="false" stereotype="" use3DEffect="true">
            <y:AttributeLabel xml:space="preserve">+name:string</y:AttributeLabel>
            <y:MethodLabel xml:space="preserve">+getOrder():string
+payOrder(order:string):int</y:MethodLabel>
          </y:UML>
        </y:UMLClassNode>
      </data>
    </node>
    <node id="n2">
      <data key="d3" xml:space="preserve"/>
      <data key="d4"/>
      <data key="d5">
        <y:UMLClassNode>
          <y:Geometry height="86.0" width="100.0" x="1086.0" y="197.0"/>
          <y:Fill color="#FFCC00" transparent="false"/>
          <y:BorderStyle color="#000000" type="line" width="1.0"/>
          <y:NodeLabel alignment="center" autoSizePolicy="content" fontFamily="Dialog" fontSize="13" fontStyle="bold" hasBackgroundColor="false" hasLineColor="false" height="19.92626953125" horizontalTextPosition="center" iconTextGap="4" modelName="custom" textColor="#000000" verticalTextPosition="bottom" visible="true" width="32.9072265625" x="33.54638671875" xml:space="preserve" y="3.0">User<y:LabelModel><y:SmartNodeLabelModel distance="4.0"/></y:LabelModel><y:ModelParameter><y:SmartNodeLabelModelParameter labelRatioX="0.0" labelRatioY="0.0" nodeRatioX="0.0" nodeRatioY="0.0" offsetX="0.0" offsetY="0.0" upX="0.0" upY="-1.0"/></y:ModelParameter></y:NodeLabel>
          <y:UML clipContent="true" constraint="" hasDetailsColor="false" omitDetails="false" stereotype="" use3DEffect="true">
            <y:AttributeLabel xml:space="preserve">+name:string</y:AttributeLabel>
            <y:MethodLabel xml:space="preserve">getName()</y:MethodLabel>
          </y:UML>
        </y:UMLClassNode>
      </data>
    </node>
  </graph>
  <data key="d6">
    <y:Resources/>
  </data>
</graphml>


public List<string> getNodeAttributeValue(XmlReader reader)
        {
            List<string> id = new List<string>();

            while (reader.Read())
            {
                reader.MoveToAttribute("id");
                while (reader.ReadAttributeValue())
                {
                    if (xmlReader.Value.Contains('n'))
                    {
                        string idElement = xmlReader.Value;
                        id.Add(idElement);
                        return id;
                    }
                }
            }
            return id;
        }

当我编译这个时,id总是空的。通过断点和缓慢编译,我可以看到reader.Value是:n0\\n但是为什么这个值不通过if语句?它包含一个“n”字符


空格是否有问题?如果是,如何在不创建新的读卡器的情况下向读卡器添加空白设置?阅读信息后,只需添加设置并删除它们。

我将使用xml linq并创建一个使用id作为键的字典

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Net;

namespace ConsoleApplication1
{
    class Program
    {
        const string filename = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(filename);
            XNamespace ns = doc.Root.GetDefaultNamespace();
            XNamespace yns = doc.Root.GetNamespaceOfPrefix("y");
            List<Key> keys = doc.Descendants(ns + "key").Select(x => new Key()
            {
                _for = (string)x.Attribute("for"),
                attrName = (string)x.Attribute("attr.name"),
                id = (string)x.Attribute("id"),
                fileType = (string)x.Attribute("yfiles.type")
            }).ToList();

            Dictionary<string, Key> dict = keys.GroupBy(x => x.id, y => y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

            Dictionary<string, List<string>> dict2 = doc.Descendants(ns + "node")
                .SelectMany(x => x.Elements(ns + "data").Select(y => new { id = (string)x.Attribute("id"), key = (string)y.Attribute("key")}))
                .GroupBy(x => x.key, y => y.id)
                .ToDictionary(x => x.Key, y => y.ToList());

            foreach (KeyValuePair<string, List<string>> node in dict2)
            {
                Key key = dict[node.Key];
                key.nodes = node.Value.ToArray();
            }

        }
    }
    public class Key
    {
        public string _for { get; set; }
        public string attrName { get; set; }
        public string id { get; set; }
        public string fileType { get; set; }
        public string[] nodes { get; set; }
    }
}

我不能重现你的问题,如果情况正常,我只是跳过它。这可能是因为我使用了XmlTextReader吗?因为我正在将XmlTextReader对象传递到此方法中。您刚才使用了XmlReader.Createfilepath吗?是的,我只使用了XmlReader,因为它在您的示例中使用。另外,从方法返回列表也没有意义,您可以中断循环并返回单个id值,您可以返回一个字符串simplyYou有一个必须包含的名称空间。@jdweng为什么需要包含它?Sry正在试图弄清楚整个标记是如何工作的^^^但我总是跳过这些XNamespace thingsHow我只能将字典中键对象的Id传递给方法吗?我有getModelXmlReader阅读器,字符串id需要id。我试过了,效果很好,但是什么时候我会有多个id元素呢?你打算有重复的id号吗?我可以把它列在字典里,但我不知道;I don’我认为在这种情况下是没有意义的。但是有很多像n0、n1、N2这样的id,对于每个id我都需要调用getModelmethod;你需要像我使用ns一样使用yns。