XmlValidatingReader.IsEmptyElement プロパティ

定義

現在のノードが空の要素 (<MyElement/> など) かどうかを示す値を取得します。

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

プロパティ値

Boolean

true 現在のノードが要素 (NodeType 等しい XmlNodeType.Element) で終わる />場合は、 falseそれ以外の場合は .

次の例では、各要素のテキストコンテンツを表示します。

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlTextReader^ txtreader = nullptr;
   XmlValidatingReader^ reader = nullptr;
   try
   {
      
      //Implement the readers.
      txtreader = gcnew XmlTextReader( "elems.xml" );
      reader = gcnew XmlValidatingReader( txtreader );
      
      //Parse the XML and display the text content of each of the elements.
      while ( reader->Read() )
      {
         if ( reader->IsStartElement() )
         {
            if ( reader->IsEmptyElement )
                        Console::WriteLine( "<{0}/>", reader->Name );
            else
            {
               Console::Write( "<{0}> ", reader->Name );
               reader->Read(); //Read the start tag.
               if ( reader->IsStartElement() )
                              
               //Handle nested elements.
               Console::Write( "\r\n<{0}>", reader->Name );
               Console::WriteLine( reader->ReadString() ); //Read the text content of the element.
            }
         }
      }
   }
   finally
   {
      if ( reader != nullptr )
            reader->Close();
   }

}

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

public class Sample
{
  public static void Main()
  {
    XmlTextReader txtreader = null;
    XmlValidatingReader reader = null;

    try
    {
       //Implement the readers.
       txtreader = new XmlTextReader("elems.xml");
       reader = new XmlValidatingReader(txtreader);

       //Parse the XML and display the text content of each of the elements.
       while (reader.Read()){
         if (reader.IsStartElement()){
           if (reader.IsEmptyElement)
                    {
                        Console.WriteLine("<{0}/>", reader.Name);
                    }
                    else
                    {
               Console.Write("<{0}> ", reader.Name);
               reader.Read(); //Read the start tag.
               if (reader.IsStartElement())  //Handle nested elements.
                   Console.Write("\r\n<{0}>", reader.Name);
               Console.WriteLine(reader.ReadString());  //Read the text content of the element.
           }
         }
       }
     }

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

Public Class Sample
   
   Public Shared Sub Main()
      Dim txtreader As XmlTextReader = Nothing
      Dim reader As XmlValidatingReader = Nothing
      
      Try
         'Implement the readers.
         txtreader = New XmlTextReader("elems.xml")
         reader = New XmlValidatingReader(txtreader)
         
         'Parse the XML and display the text content of each of the elements.
         While reader.Read()
            If reader.IsStartElement() Then
               If reader.IsEmptyElement Then
                  Console.WriteLine("<{0}/>", reader.Name)
               Else
                  Console.Write("<{0}> ", reader.Name)
                  reader.Read() 'Read the start tag.
                  If (reader.IsStartElement())  'Handle nested elements.
                    Console.WriteLine()
                    Console.Write("<{0}>", reader.Name)
                  End If
                  Console.WriteLine(reader.ReadString()) 'Read the text content of the element.
               End If
            End If
         End While      
      
      Finally
         If Not (reader Is Nothing) Then
            reader.Close()
         End If
      End Try
   End Sub
End Class

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

<book>
  <title>Pride And Prejudice</title>
  <price>19.95</price>
  <misc/>
</book>

注釈

注意

このクラスはXmlValidatingReader、.NET Framework 2.0 では廃止されています。 クラスとメソッドを使用してXmlReaderSettings、検証XmlReaderインスタンスをCreate作成できます。 詳細については、XmlReader のリファレンス ページの「解説」を参照してください。

このプロパティにより、次のことが区別できます。

<item num="123"/> (IsEmptyElement is true)。

<item num="123"> (IsEmptyElementfalse、要素の内容が空ですが)。

対応する EndElement ノードは、空の要素には生成されません。

スキーマの検証のために既定のコンテンツが要素に追加された場合でもtrueIsEmptyElement. 要素に既定値があるかどうかは関係ありません。 つまり、 IsEmptyElement ソース ドキュメント内の要素に終了要素タグがあるかどうかを報告するだけです。

適用対象

こちらもご覧ください