XmlTextReader.HasValue 屬性

定義

得到一個值,表示目前節點是否可以有 ValueString.Empty以外的 。

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

屬性值

true如果讀取器目前所在的節點可以有 Value;否則,。 false

範例

以下範例顯示每個可有值的節點的值。

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>

備註

Note

建議您使用 XmlReader 方法來建立XmlReader.Create實例,以利用新功能。

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

節點類型 價值觀
Attribute 屬性的值。
CDATA CDATA 章節的內容。
Comment 批注的內容。
DocumentType 內部子集。
ProcessingInstruction 整個內容,不包括目標。
SignificantWhitespace 混合內容模型中標記之間的空白。
Text 文字節點的內容。
Whitespace 標記之間的空白。
XmlDeclaration 宣言的內容。

適用於

另請參閱