XmlTextReader.HasValue XmlTextReader.HasValue XmlTextReader.HasValue XmlTextReader.HasValue Property

定义

获取一个值,该值指示当前节点是否可以具有 String.Empty 以外的 ValueGets a value indicating whether the current node can have a Value other than String.Empty.

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

属性值

如果读取器当前定位在的节点可以具有 Value,则为 true;否则为 falsetrue if the node on which the reader is currently positioned can have a Value; otherwise, false.

示例

下面的示例显示了可以具有值的每个节点的值。The following example displays the value for every node that can have a value.

#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 'Main
End Class 'Sample 

该示例使用文件book1.xml作为输入。The example uses the file, book1.xml, as input.


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

注解

备注

从 .NET Framework 2.0 开始, 我们建议XmlReader XmlReader.Create使用方法创建实例以利用新功能。Starting with the .NET Framework 2.0, we recommend that you create XmlReader instances by using the XmlReader.Create method to take advantage of new functionality.

下表列出具有要返回的值的节点类型。The following table lists node types that have a value to return.

节点类型Node type Value
Attribute 属性的值。The value of the attribute.
CDATA CDATA 节的内容。The content of the CDATA section.
Comment 注释的内容。The content of the comment.
DocumentType 内部子集。The internal subset.
ProcessingInstruction 全部内容(不包括指令目标)。The entire content, excluding the target.
SignificantWhitespace 混合内容模型中标记之间的空白。The white space between markup in a mixed content model.
Text 文本节点的内容。The content of the text node.
Whitespace 标记之间的空白。The white space between markup.
XmlDeclaration 声明的内容。The content of the declaration.

适用于

另请参阅