XmlReader.Read XmlReader.Read XmlReader.Read XmlReader.Read Method

定义

当在派生类中被重写时,从流中读取下一个节点。When overridden in a derived class, reads the next node from the stream.

public:
 abstract bool Read();
public abstract bool Read ();
abstract member Read : unit -> bool
Public MustOverride Function Read () As Boolean

返回

如果已成功读取下一节点,则为 true;否则为 falsetrue if the next node was read successfully; otherwise, false.

异常

分析 XML 时出错。An error occurred while parsing the XML.

在上一次异步操作完成之前调用了 XmlReader 方法。An XmlReader method was called before a previous asynchronous operation finished. 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”In this case, InvalidOperationException is thrown with the message "An asynchronous operation is already in progress."

示例

下面的示例读取 XML 文件并显示每个节点:The following example reads an XML file and displays each of the nodes:

XmlReaderSettings settings = new XmlReaderSettings();
settings.DtdProcessing = DtdProcessing.Parse;
XmlReader reader = XmlReader.Create("items.xml", settings);
  
reader.MoveToContent();
  // Parse the file and display each of the nodes.
  while (reader.Read()) {
    switch (reader.NodeType) {
      case XmlNodeType.Element:
          Console.Write("<{0}>", reader.Name);
          break;
      case XmlNodeType.Text:
          Console.Write(reader.Value);
          break;
       case XmlNodeType.CDATA:
           Console.Write("<![CDATA[{0}]]>", reader.Value);
           break;
       case XmlNodeType.ProcessingInstruction:
           Console.Write("<?{0} {1}?>", reader.Name, reader.Value);
           break;
       case XmlNodeType.Comment:
           Console.Write("<!--{0}-->", reader.Value);
           break;
       case XmlNodeType.XmlDeclaration:
           Console.Write("<?xml version='1.0'?>");
           break;
       case XmlNodeType.Document:
           break;
       case XmlNodeType.DocumentType:
           Console.Write("<!DOCTYPE {0} [{1}]", reader.Name, reader.Value);
           break;
       case XmlNodeType.EntityReference:
           Console.Write(reader.Name);
           break;
       case XmlNodeType.EndElement:
           Console.Write("</{0}>", reader.Name);
           break;
   } 
}
Dim settings As New XmlReaderSettings()
settings.DtdProcessing = DtdProcessing.Parse
Dim reader As XmlReader = XmlReader.Create("items.xml", settings)
reader.MoveToContent()
' Parse the file and display each of the nodes.
While reader.Read()
  Select Case reader.NodeType
    Case XmlNodeType.Element
      Console.Write("<{0}>", reader.Name)
    Case XmlNodeType.Text
      Console.Write(reader.Value)
    Case XmlNodeType.CDATA
      Console.Write("<![CDATA[{0}]]>", reader.Value)
    Case XmlNodeType.ProcessingInstruction
      Console.Write("<?{0} {1}?>", reader.Name, reader.Value)
    Case XmlNodeType.Comment
      Console.Write("<!--{0}-->", reader.Value)
    Case XmlNodeType.XmlDeclaration
      Console.Write("<?xml version='1.0'?>")
    Case XmlNodeType.Document
    Case XmlNodeType.DocumentType
      Console.Write("<!DOCTYPE {0} [{1}]", reader.Name, reader.Value)
    Case XmlNodeType.EntityReference
      Console.Write(reader.Name)
    Case XmlNodeType.EndElement
      Console.Write("</{0}>", reader.Name)
  End Select
End While

该示例使用items.xml文件。The sample uses the items.xml file.

<?xml version="1.0"?>
<!-- This is a sample XML document -->
<!DOCTYPE Items [<!ENTITY number "123">]>
<Items>
  <Item>Test with an entity: &number;</Item>
  <Item>Test with a child element <more/> stuff</Item>
  <Item>Test with a CDATA section <![CDATA[<456>]]> def</Item>
  <Item>Test with a char entity: &#65;</Item>
  <!-- Fourteen chars in this element.-->
  <Item>1234567890ABCD</Item>
</Items>

输出:Output:

<Item>Test with an entity: 123</Item><Item>Test with a child element <more> stuff</Item><Item>Test with a CDATA section <![CDATA[<456>]]> def</Item><Item>Test with a char entity: A</Item><!-- Fourteen chars in this element.--><Item>1234567890ABCD</Item></Items>

注解

XmlReader首次创建和初始化时,没有可用的信息。When an XmlReader is first created and initialized, there is no information available. 您必须调用Read以读取第一个节点。You must call Read to read the first node. 方法将 xml 读取器的状态设置为 "启动ReadState.Initial ",并按顺序在 xml 文件中移动,直到它到达文件末尾,此时false方法返回值。 ReadThe Read method sets the state of the XML reader to initiate ReadState.Initial and moves through the XML file sequentially until it reaches the end of the file, at which point the method returns a value of false.

此方法至少需要数据流中的四个字节,才能开始分析。This method requires at least four bytes from the data stream in order to begin parsing. 如果返回的字节数少于四个,并且流中没有更多的数据,则该false方法将返回。If fewer than four bytes are returned and there is no more data in the stream, the method returns false. 如果流中有更多数据,该方法将会阻止分析,直到收到第四个字节。If there is more data in the stream, the method will block parsing until receipt of the fourth byte.

有关此方法的异步版本,请参阅ReadAsyncFor the asynchronous version of this method, see ReadAsync.

适用于