XmlTextReader.HasValue Proprietà

Definizione

Ottiene un valore che indica se il nodo corrente può avere un Value valore diverso da 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

Valore della proprietà

true se il nodo in cui è posizionato il lettore può avere un Valueoggetto ; in caso contrario, false.

Esempio

Nell'esempio seguente viene visualizzato il valore per ogni nodo che può avere un valore.

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

Nell'esempio viene usato il file , book1.xml, come input.


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

Commenti

Note

È consigliabile creare XmlReader istanze usando il XmlReader.Create metodo per sfruttare le nuove funzionalità.

Nella tabella seguente sono elencati i tipi di nodo con un valore da restituire.

Tipo di nodo Value
Attribute Valore dell'attributo.
CDATA Contenuto della sezione CDATA.
Comment Contenuto del commento.
DocumentType Subset interno.
ProcessingInstruction L'intero contenuto, escluso la destinazione.
SignificantWhitespace Spazio vuoto tra markup in un modello di contenuto misto.
Text Contenuto del nodo di testo.
Whitespace Spazio vuoto tra markup.
XmlDeclaration Contenuto della dichiarazione.

Si applica a

Vedi anche