XmlTextReader.HasValue 屬性

定義

取得值,表示目前的節點是否可以具有 String.Empty 之外的其他 Value

public:
 virtual property bool HasValue { bool get(); };
public override bool HasValue { get; }
member this.HasValue : bool
Public Overrides ReadOnly Property HasValue As Boolean

屬性值

Boolean

如果讀取器目前所在節點具有 Value,則為 true,否則為 false

範例

下列範例會顯示每個可具有值之節點的值。

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlTextReader^ reader = nullptr;
   try
   {
      
      //Load the reader with the XML file.
      reader = gcnew XmlTextReader( "book1.xml" );
      reader->WhitespaceHandling = WhitespaceHandling::None;
      
      //Parse the file and display each node.
      while ( reader->Read() )
      {
         if ( reader->HasValue )
                  Console::WriteLine( "({0})  {1}={2}", reader->NodeType, reader->Name, reader->Value );
         else
                  Console::WriteLine( "({0}) {1}", reader->NodeType, reader->Name );
      }
   }
   finally
   {
      if ( reader != nullptr )
            reader->Close();
   }

}
using System;
using System.IO;
using System.Xml;

public class Sample
{

  public static void Main()
  {
    XmlTextReader reader = null;

    try
    {
        //Load the reader with the XML file.
        reader = new XmlTextReader("book1.xml");
        reader.WhitespaceHandling = WhitespaceHandling.None;

        //Parse the file and display each node.
        while (reader.Read())
        {
           if (reader.HasValue)
             Console.WriteLine("({0})  {1}={2}", reader.NodeType, reader.Name, reader.Value);
           else
             Console.WriteLine("({0}) {1}", reader.NodeType, reader.Name);
         }
     }

     finally
     {
       if (reader!=null)
         reader.Close();
     }
  }
} // End class
Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        Dim reader As XmlTextReader = Nothing
        
        Try
            'Load the reader with the XML file.
            reader = New XmlTextReader("book1.xml")
            reader.WhitespaceHandling = WhitespaceHandling.None
            
            'Parse the file and display each node.
            While reader.Read()
                If reader.HasValue Then
                    Console.WriteLine("({0})  {1}={2}", reader.NodeType, reader.Name, reader.Value)
                Else
                    Console.WriteLine("({0}) {1}", reader.NodeType, reader.Name)
                End If
            End While
        
        Finally
            If Not (reader Is Nothing) Then
                reader.Close()
            End If
        End Try
    End Sub
End Class

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


<?xml version='1.0' ?>
<!DOCTYPE book [<!ENTITY h 'hardcover'>]>
<book>
  <title>Pride And Prejudice</title>
  <misc>&h;</misc>
</book>

備註

注意

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

下表列出具有傳回值的節點類型。

節點類型
Attribute 屬性的值。
CDATA CDATA 區段的內容。
Comment 註解的內容。
DocumentType 內部子集。
ProcessingInstruction 全部內容 (目標除外)。
SignificantWhitespace 在混合內容模型中標記間的泛空白字元。
Text 文字節點的內容。
Whitespace 標記之間的泛空白字元。
XmlDeclaration 宣告的內容。

適用於

另請參閱