Leer en inglés Editar

Compartir a través de


XmlTextReader.ResetState Method

Definition

Resets the state of the reader to ReadState.Initial.

public void ResetState ();

Exceptions

Calling ResetState if the reader was constructed using an XmlParserContext.

Documents in a single stream do not share the same encoding.

Examples

The following example parses two XML documents in a single stream.

using System;
using System.IO;
using System.Text;
using System.Xml;

public class Sample
{
  public static void Main(){

     Encoding enc = new UTF8Encoding();
     byte[] utf8Buffer = enc.GetBytes("<root> 12345 </root>");

     enc = new UnicodeEncoding();
     byte[] unicodeBuffer = enc.GetBytes("<?xml version='1.0' ?><unicode> root </unicode>");

     MemoryStream memStrm = new MemoryStream();
     memStrm.Write(unicodeBuffer, 0, unicodeBuffer.Length);
     memStrm.Write(utf8Buffer, 0, utf8Buffer.Length);
     memStrm.Position = 0;

     XmlTextReader reader = new XmlTextReader(memStrm);

     while(reader.Read()) {
        Console.WriteLine("NodeType: {0}", reader.NodeType);
        if (XmlNodeType.EndElement == reader.NodeType && "root" == reader.Name) {
          break;
        }

        if (XmlNodeType.EndElement == reader.NodeType) {
          reader.ResetState();
       }
    }
  }
}

Remarks

Nota

Starting with the .NET Framework 2.0, we recommend that you create XmlReader instances by using the XmlReader.Create method to take advantage of new functionality.

This method enables you to parse multiple XML documents in a single stream. When you reach the end of an XML document, you can call ResetState to reset the state of the reader in preparation for the next XML document.

Importante

The documents in the stream must share the same encoding. If this is not the case, when ResetState is called an XmlException is thrown. (This is a change in behavior from .NET Framework version 1.1 and earlier).

The following properties are not affected by ResetState.

Applies to

Producto Versiones
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

See also