共用方式為


XmlTextReader.XmlSpace 屬性

定義

取得目前的 xml:space 範圍。

public:
 virtual property System::Xml::XmlSpace XmlSpace { System::Xml::XmlSpace get(); };
public override System.Xml.XmlSpace XmlSpace { get; }
member this.XmlSpace : System.Xml.XmlSpace
Public Overrides ReadOnly Property XmlSpace As XmlSpace

屬性值

XmlSpace

其中一個 XmlSpace 值。 如果 xml:space 範圍不存在,這個屬性預設值為 XmlSpace.None

範例

下列範例會剖析檔案,並在找到範圍時 xml:space='preserve' 傳回顯著的空白字元。

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlTextReader^ reader = gcnew XmlTextReader( "authors.xml" );
   reader->WhitespaceHandling = WhitespaceHandling::None;
   
   // Parse the file.  Return white space only if an
   // xml:space='preserve' attribute is found.
   while ( reader->Read() )
   {
      switch ( reader->NodeType )
      {
         case XmlNodeType::Element:
            Console::Write( "<{0}>", reader->Name );
            if ( reader->XmlSpace == XmlSpace::Preserve )
                        reader->WhitespaceHandling = WhitespaceHandling::Significant;
            break;

         case XmlNodeType::Text:
            Console::Write( reader->Value );
            break;

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

         case XmlNodeType::SignificantWhitespace:
            Console::Write( reader->Value );
            break;
      }
   }
}
using System;
using System.IO;
using System.Xml;

public class Sample{

  public static void Main(){

     XmlTextReader reader = new XmlTextReader("authors.xml");
     reader.WhitespaceHandling = WhitespaceHandling.None;

     // Parse the file.  Return white space only if an
     // xml:space='preserve' attribute is found.
     while (reader.Read()){
       switch (reader.NodeType){
         case XmlNodeType.Element:
           Console.Write("<{0}>", reader.Name);
           if (reader.XmlSpace==XmlSpace.Preserve)
             reader.WhitespaceHandling=WhitespaceHandling.Significant;
           break;
         case XmlNodeType.Text:
           Console.Write(reader.Value);
           break;
         case XmlNodeType.EndElement:
           Console.Write("</{0}>", reader.Name);
           break;
         case XmlNodeType.SignificantWhitespace:
           Console.Write(reader.Value);
           break;
       }
     }
  }
}
Imports System.IO
Imports System.Xml

public class Sample

  public shared sub Main()

     Dim reader as XmlTextReader = new XmlTextReader("authors.xml")
     reader.WhitespaceHandling = WhitespaceHandling.None

     ' Parse the file.  Return white space only if an
     ' xml:space='preserve' attribute is found.
     while (reader.Read())
       select case reader.NodeType
         case XmlNodeType.Element:
           Console.Write("<{0}>", reader.Name)
           if (reader.XmlSpace=XmlSpace.Preserve)
             reader.WhitespaceHandling=WhitespaceHandling.Significant
           end if
         case XmlNodeType.Text:
           Console.Write(reader.Value)
         case XmlNodeType.EndElement:
           Console.Write("</{0}>", reader.Name)
         case XmlNodeType.SignificantWhitespace:
           Console.Write(reader.Value)        
       end select              
     end while
  end sub
end class

此範例會使用 檔案 authors.xml ,作為輸入。

備註

注意

從 .NET Framework 2.0 開始,建議您使用 XmlReader.Create 方法來建立 XmlReader 實例,以利用新功能。

適用於

另請參閱