C#XmlReader无法读取“

问题描述 投票:0回答:1

我写了一个XML解析器,在InnerXml中,我得到了这样的文本:

<Name ...><<interface>>Employee</Name>
<!-- "<<interface>>Employee" is the InnerXml Text and I need it as string -->

我的阅读文字代码如下:

        public ClassObject AnalyzeName(XmlReader reader)
        {
            // new Class object where the name will be stored
           ClassObject classObject = new ClassObject();

            // XMLReader 
            while (reader.Read())
            {
                if (reader.Name == "Name" && reader.NodeType == XmlNodeType.Element)
                {
                    className = getName(reader.ReadSubtree());
                    classObject.name = className;
                }
           }
         return classObject;
       }



     string getName(XmlReader reader)
        {
            string className;
            while (reader.Read())
            {
                if (reader.HasValue)
                {
                    className += reader.Value;
                }
            }
            return className;
        }

但是在我的XmlTextReader阅读此行之后,我得到一个例外:

Nachricht: 
    System.Xml.XmlException : Ein Name darf nicht mit dem Zeichen '<', hexadezimaler Wert 0x3C, beginnen.

这意味着'

<Name...>&lt;&lt;interface&gt;&gt;Employee</Name> <!-- instead of <<interface>> -->

整个.grapml文件(这是一个不同的xml表示法:(位于y:NodeLabel)

<?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="33.40234375" horizontalTextPosition="center" iconTextGap="4" modelName="internal" modelPosition="c" textColor="#000000" verticalTextPosition="bottom" visible="true" width="82.052734375" x="24.4736328125" 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():Int
getRoom():String
getPhone()</y:MethodLabel>
          </y:UML>
        </y:UMLClassNode>
      </data>
    </node>
  </graph>
  <data key="d6">
    <y:Resources/>
  </data>
</graphml>

c# xml exception xml-parsing xmlexception
1个回答
0
投票
在评论之后,我写了一个简单的代码片段,效果很好

var xmlReader = XmlReader.Create("..."); bool canRead = xmlReader.ReadToDescendant("y:NodeLabel"); if (canRead) { var content = xmlReader.ReadElementContentAsString(); }

© www.soinside.com 2019 - 2024. All rights reserved.