XmlTextReader.WhitespaceHandling Właściwość

Definicja

Pobiera lub ustawia wartość określającą sposób obsługi odstępu.

public:
 property System::Xml::WhitespaceHandling WhitespaceHandling { System::Xml::WhitespaceHandling get(); void set(System::Xml::WhitespaceHandling value); };
public System.Xml.WhitespaceHandling WhitespaceHandling { get; set; }
member this.WhitespaceHandling : System.Xml.WhitespaceHandling with get, set
Public Property WhitespaceHandling As WhitespaceHandling

Wartość właściwości

WhitespaceHandling Jedna z wartości. Wartość domyślna to WhitespaceHandling.All (zwraca Whitespace i SignificantWhitespace węzły).

Wyjątki

Określono nieprawidłową wartość.

Ustawienie tej właściwości po zamknięciu czytnika (ReadState to ReadState.Closed).

Przykłady

Poniższy przykład odczytuje fragment XML.

#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 );
}
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
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

Uwagi

Uwaga

Począwszy od .NET Framework 2.0, zalecamy utworzenie XmlReader wystąpień przy użyciu XmlReader.Create metody , aby korzystać z nowych funkcji.

Tę właściwość można zmienić w dowolnym momencie i będzie obowiązywać przy następnej operacji odczytu.

Ponieważ obiekt XmlTextReader nie ma dostępnych informacji o jednostkach DTD, SignificantWhitespace węzły są zwracane tylko w xml:space='preserve' zakresie.

Dotyczy

Zobacz też