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

属性值

true如果当前节点是元素 (NodeType等于以 结尾/>的) ,则为 ;否则为 falseXmlNodeType.Element

示例

以下示例显示每个元素的文本内容。

#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 中已过时。 可以使用 类和 Create 方法创建验证XmlReader实例XmlReaderSettings。 有关详细信息,请参阅 XmlReader 引用页的“备注”部分。

此属性使您能够确定下面各项之间的差异:

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

<item num="123"> IsEmptyElement (为 false,但元素内容) 为空。

不会为空元素生成相应的 EndElement 节点。

如果由于架构验证而将默认内容添加到元素中, IsEmptyElement 则 仍返回 true。 它与元素是否具有默认值没有关系。 换句话说, IsEmptyElement 只需报告源文档中的元素是否具有结束元素标记。

适用于

另请参阅