XmlTextReader.ResetState Metodo

Definizione

Reimposta lo stato del lettore su ReadState.Initial.Resets the state of the reader to ReadState.Initial.

public:
 void ResetState();
public void ResetState ();
member this.ResetState : unit -> unit
Public Sub ResetState ()

Eccezioni

Chiamata a ResetState se il visualizzatore è stato costruito con un oggetto XmlParserContext.Calling ResetState if the reader was constructed using an XmlParserContext.

I documenti di uno stesso flusso non condividono la stessa codifica.Documents in a single stream do not share the same encoding.

Esempio

Nell'esempio seguente vengono analizzati due documenti XML in un singolo flusso.The following example parses two XML documents in a single stream.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Text;
using namespace System::Xml;
int main()
{
   Encoding^ enc = gcnew UTF8Encoding;
   array<Byte>^utf8Buffer = enc->GetBytes( "<root> 12345 </root>" );
   enc = gcnew UnicodeEncoding;
   array<Byte>^unicodeBuffer = enc->GetBytes( "<?xml version='1.0' ?><unicode> root </unicode>" );
   MemoryStream^ memStrm = gcnew MemoryStream;
   memStrm->Write( unicodeBuffer, 0, unicodeBuffer->Length );
   memStrm->Write( utf8Buffer, 0, utf8Buffer->Length );
   memStrm->Position = 0;
   XmlTextReader^ reader = gcnew 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();
   }
}

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();
       }
    } 
  }
}
Imports System.IO
Imports System.Text
Imports System.Xml

public class Sample

  public shared sub Main()

     Dim enc as Encoding = new UTF8Encoding()
     Dim utf8Buffer as byte() = enc.GetBytes("<root> 12345 </root>") 

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

     Dim memSreaderm as MemoryStream = new MemoryStream()
     memSreaderm.Write(unicodeBuffer, 0, unicodeBuffer.Length)
     memSreaderm.Write(utf8Buffer, 0, utf8Buffer.Length)
     memSreaderm.Position = 0

     Dim reader as XmlTextReader = new XmlTextReader(memSreaderm)

     while(reader.Read()) 
        Console.WriteLine("NodeType: {0}", reader.NodeType)
        if (XmlNodeType.EndElement = reader.NodeType And "root" = reader.Name) 
         exit while
        end if
        
        if (XmlNodeType.EndElement = reader.NodeType) 
          reader.ResetState()
       end if
    end while

  end sub
end class

Commenti

Nota

A partire dalla .NET Framework 2,0, è consigliabile creare istanze di XmlReader usando il metodo XmlReader.Create per sfruttare le nuove funzionalità.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.

Questo metodo consente di analizzare più documenti XML in un singolo flusso.This method enables you to parse multiple XML documents in a single stream. Quando si raggiunge la fine di un documento XML, è possibile chiamare ResetState per reimpostare lo stato del lettore in preparazione al documento XML successivo.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

I documenti nel flusso devono condividere la stessa codifica.The documents in the stream must share the same encoding. In caso contrario, quando ResetState viene chiamato viene generata un'XmlException.If this is not the case, when ResetState is called an XmlException is thrown. Si tratta di una modifica del comportamento da .NET Framework versione 1,1 e precedenti).(This is a change in behavior from .NET Framework version 1.1 and earlier).

Le proprietà seguenti non sono interessate dalla ResetState.The following properties are not affected by ResetState.

Si applica a

Vedi anche