次の方法で共有


XmlTextReader.HasValue プロパティ

現在のノードが Value を持つことができるかどうかを示す値を取得します。

Overrides Public ReadOnly Property HasValue As Boolean
[C#]
public override bool HasValue {get;}
[C++]
public: __property bool get_HasValue();
[JScript]
public override function get HasValue() : Boolean;

プロパティ値

リーダーが現在配置されているノードが値を持つことができる場合は true 。それ以外の場合は false

解説

返す値を持つノード型の一覧を次の表に示します。

ノード型
Attribute 属性の値。
CDATA CDATA セクションの内容。
Comment コメントの内容。
DocumentType 内部サブセット。
ProcessingInstruction ターゲットを含まない、全体の内容。
SignificantWhitespace 混合コンテンツ モデル内のマークアップ間の空白。
Text テキスト ノードの内容。
Whitespace マークアップ間の空白。
XmlDeclaration 宣言の内容。

使用例

[Visual Basic, C#, C++] 値を持つことができるすべてのノードの値を表示する例を次に示します。

 
Imports System
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 

[C#] 
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

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;

int main()
{
   XmlTextReader* reader = 0;

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

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

   __finally
   {
      if (reader!=0)
         reader->Close();
   }
}

この例では、入力として、 book1.xml というファイルを使用しています。

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

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

XmlTextReader クラス | XmlTextReader メンバ | System.Xml 名前空間