Proprietà XmlTextReader.WhitespaceHandling (System.Xml)

Cambia visualizzazione:
ScriptFree
Riferimento a .NET Framework
Proprietà XmlTextReader.WhitespaceHandling

Ottiene o imposta un valore che specifica la modalità di gestione dello spazio vuoto.

Spazio dei nomi: System.Xml
Assembly: System.Xml (in system.xml.dll)

Sintassi

Visual Basic - (Dichiarazione)
Public Property WhitespaceHandling As WhitespaceHandling
Visual Basic (Utilizzo)
Dim instance As XmlTextReader
Dim value As WhitespaceHandling

value = instance.WhitespaceHandling

instance.WhitespaceHandling = value
C#
public WhitespaceHandling WhitespaceHandling { get; set; }
C++
public:
property WhitespaceHandling WhitespaceHandling {
	WhitespaceHandling get ();
	void set (WhitespaceHandling value);
}
J#
/** @property */
public WhitespaceHandling get_WhitespaceHandling ()

/** @property */
public void set_WhitespaceHandling (WhitespaceHandling value)

JScript
public function get WhitespaceHandling () : WhitespaceHandling

public function set WhitespaceHandling (value : WhitespaceHandling)

Valore proprietà

Uno dei valori di WhitespaceHandling. Il valore predefinito è WhitespaceHandling.All. Restituisce i nodi Whitespace e SignificantWhitespace.
Eccezioni

Tipo di eccezione Condizione

ArgumentOutOfRangeException

È stato specificato un valore non valido.

InvalidOperationException

Impostare questa proprietà quando il visualizzatore è chiuso (ReadState è ReadState.Closed).

Note

NotaNota

Nella versione Microsoft .NET Framework versione 2.0 è consigliabile creare istanze di XmlReader utilizzando il metodo System.Xml.XmlReader.Create. In questo modo è possibile sfruttare completamente le nuove funzionalità introdotte in questa versione. Per ulteriori informazioni, vedere Creazione di lettori XML.

È possibile modificare questa proprietà in qualsiasi momento e rendere effettive le modifiche durante la successiva operazione di lettura.

Poiché per XmlTextReader non sono disponibili informazioni DTD, i nodi SignificantWhitepsace vengono restituiti solo all'interno di un ambito xml:space='preserve'.

Esempio

Nell'esempio seguente viene letto un frammento XML

Visual Basic
Imports System
Imports System.IO
Imports System.Xml

public class Sample 

  public shared sub Main()

    'Create the XML fragment to be parsed.
    Dim xmlFrag as string ="<book> " & _
                           "  <title>Pride And Prejudice</title>" & _
                           "  <genre>novel</genre>" & _
                           "</book>" 

    'Create the XmlNamespaceManager.
    Dim nt as NameTable = new NameTable()
    Dim nsmgr as XmlNamespaceManager = new XmlNamespaceManager(nt)

    'Create the XmlParserContext.
    Dim context as XmlParserContext = new XmlParserContext(nothing, nsmgr, nothing, XmlSpace.Default)

    Console.WriteLine("Read the XML and ignore all white space...")
    ReadXML(context, xmlFrag, WhitespaceHandling.None)

    Console.WriteLine()
    Console.WriteLine("Read the XML including white space nodes...")
    ReadXML(context, xmlFrag, WhitespaceHandling.All)
  end sub
  
  public shared sub ReadXML(context as XmlParserContext, xmlFrag as string, ws as WhitespaceHandling)

    'Create the reader and specify the WhitespaceHandling setting.
    Dim reader as XmlTextReader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context)
    reader.WhitespaceHandling = ws

      'Parse the XML and display each of the nodes.
      while (reader.Read())
         select case reader.NodeType
           case XmlNodeType.Element:
             Console.WriteLine("{0}: <{1}>", reader.NodeType, reader.Name)
           case XmlNodeType.Text:
             Console.WriteLine("{0}: {1}", reader.NodeType, reader.Value)
           case XmlNodeType.EndElement:
             Console.WriteLine("{0}: </{1}>", reader.NodeType, reader.Name)
           case XmlNodeType.Whitespace:
             Console.WriteLine("{0}:", reader.NodeType)
           case XmlNodeType.SignificantWhitespace:
             Console.WriteLine("{0}:", reader.NodeType)
         end select       
      end while           
  
    'Close the reader.
    reader.Close()     
  
  end sub
end class


C#
using System;
using System.IO;
using System.Xml;

public class Sample 
{
  public static void Main(){

    //Create the XML fragment to be parsed.
    string xmlFrag ="<book> " +
                    "  <title>Pride And Prejudice</title>" +
                    "  <genre>novel</genre>" +
                    "</book>"; 

    //Create the XmlNamespaceManager.
    NameTable nt = new NameTable();
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);

    //Create the XmlParserContext.
    XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.Default);

    Console.WriteLine("Read the XML and ignore all white space...");
    ReadXML(context, xmlFrag, WhitespaceHandling.None);

    Console.WriteLine("\r\nRead the XML including white space nodes...");
    ReadXML(context, xmlFrag, WhitespaceHandling.All);

  }
  
  public static void ReadXML(XmlParserContext context, string xmlFrag, WhitespaceHandling ws){

    //Create the reader and specify the WhitespaceHandling setting.
    XmlTextReader reader = new XmlTextReader(xmlFrag, XmlNodeType.Element, context);
    reader.WhitespaceHandling = ws;

      //Parse the XML and display each of the nodes.
      while (reader.Read())
      {
         switch (reader.NodeType)
         {
           case XmlNodeType.Element:
             Console.WriteLine("{0}: <{1}>", reader.NodeType, reader.Name);
             break;
           case XmlNodeType.Text:
             Console.WriteLine("{0}: {1}", reader.NodeType, reader.Value);
             break;
           case XmlNodeType.EndElement:
             Console.WriteLine("{0}: </{1}>", reader.NodeType, reader.Name);
             break;
           case XmlNodeType.Whitespace:
             Console.WriteLine("{0}:", reader.NodeType);
             break;
           case XmlNodeType.SignificantWhitespace:
             Console.WriteLine("{0}:", reader.NodeType);
             break;
         }       
      }           
  
    //Close the reader.
    reader.Close();     
  
  }
} // End class


C++
#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
void ReadXML( XmlParserContext^ context, String^ xmlFrag, WhitespaceHandling ws )
{
   
   //Create the reader and specify the WhitespaceHandling setting.
   XmlTextReader^ reader = gcnew XmlTextReader( xmlFrag,XmlNodeType::Element,context );
   reader->WhitespaceHandling = ws;
   
   //Parse the XML and display each of the nodes.
   while ( reader->Read() )
   {
      switch ( reader->NodeType )
      {
         case XmlNodeType::Element:
            Console::WriteLine( "{0}: <{1}>", reader->NodeType, reader->Name );
            break;

         case XmlNodeType::Text:
            Console::WriteLine( "{0}: {1}", reader->NodeType, reader->Value );
            break;

         case XmlNodeType::EndElement:
            Console::WriteLine( "{0}: </{1}>", reader->NodeType, reader->Name );
            break;

         case XmlNodeType::Whitespace:
            Console::WriteLine( "{0}:", reader->NodeType );
            break;

         case XmlNodeType::SignificantWhitespace:
            Console::WriteLine( "{0}:", reader->NodeType );
            break;
      }
   }

   
   //Close the reader.
   reader->Close();
}

int main()
{
   
   //Create the XML fragment to be parsed.
   String^ xmlFrag = "<book> "
   "  <title>Pride And Prejudice</title>"
   "  <genre>novel</genre>"
   "</book>";
   
   //Create the XmlNamespaceManager.
   NameTable^ nt = gcnew NameTable;
   XmlNamespaceManager^ nsmgr = gcnew XmlNamespaceManager( nt );
   
   //Create the XmlParserContext.
   XmlParserContext^ context = gcnew XmlParserContext( nullptr,nsmgr,nullptr,XmlSpace::Default );
   Console::WriteLine( "Read the XML and ignore all white space..." );
   ReadXML( context, xmlFrag, WhitespaceHandling::None );
   Console::WriteLine( "\r\nRead the XML including white space nodes..." );
   ReadXML( context, xmlFrag, WhitespaceHandling::All );
}


J#
import System.*;
import System.IO.*;
import System.Xml.*;

public class Sample
{
    public static void main(String[] args)
    {
        //Create the XML fragment to be parsed.
        String xmlFrag = "<book> " + "  <title>Pride And Prejudice</title>" 
		    + "  <genre>novel</genre>" + "</book>";

        //Create the XmlNamespaceManager.
        NameTable nt =  new NameTable();
        XmlNamespaceManager nsMgr =  new XmlNamespaceManager(nt);

        //Create the XmlParserContext.
        XmlParserContext context =  new XmlParserContext
            (null, nsMgr, null, XmlSpace.Default);
        Console.WriteLine("Read the XML and ignore all white space...");
        ReadXML(context, xmlFrag, WhitespaceHandling.None);
        Console.WriteLine("\r\nRead the XML including white space nodes...");
        ReadXML(context, xmlFrag, WhitespaceHandling.All);
    } //main

    public static void ReadXML(XmlParserContext context, String xmlFrag,
        WhitespaceHandling ws)
    {
        //Create the reader and specify the WhitespaceHandling setting.
        XmlTextReader reader = 
            new XmlTextReader(xmlFrag, XmlNodeType.Element, context);
        reader.set_WhitespaceHandling(ws);

        //Parse the XML and display each of the nodes.
        while(reader.Read()) {
            switch(reader.get_NodeType()) {
                case XmlNodeType.Element : 
                    Console.WriteLine("{0}: <{1}>", 
                        reader.get_NodeType(), reader.get_Name());
                    break;
                case XmlNodeType.Text : 
                    Console.WriteLine("{0}: {1}", 
                        reader.get_NodeType(), reader.get_Value());
                    break;
                case XmlNodeType.EndElement : 
                    Console.WriteLine("{0}: </{1}>", 
                        reader.get_NodeType(), reader.get_Name());
                    break;
                case XmlNodeType.Whitespace : 
                    Console.WriteLine("{0}:", reader.get_NodeType());
                    break;
                case XmlNodeType.SignificantWhitespace : 
                    Console.WriteLine("{0}:", reader.get_NodeType());
                    break;
            }
        }
        //Close the reader.
        reader.Close();
    } //ReadXML 
} // End class Sample

Piattaforme

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile per Pocket PC, Windows Mobile per Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework non supporta tutte le versioni di ciascuna piattaforma. Per un elenco delle versioni supportate, vedere Requisiti di sistema.

Informazioni sulla versione

.NET Framework

Supportato in: 2.0 1.1 1.0

.NET Compact Framework

Supportato in: 2.0 1.0
Vedere anche