XmlElement.IsEmpty 屬性

定義

取得或設定項目的標記格式。

public:
 property bool IsEmpty { bool get(); void set(bool value); };
public bool IsEmpty { get; set; }
member this.IsEmpty : bool with get, set
Public Property IsEmpty As Boolean

屬性值

Boolean

true如果專案是以簡短標籤格式 「 < item/」 序列化, false 則為長格式 「 < item > < / > item > 」。

設定這個屬性時,如果設定為 true,則會移除項目的子項目,並以短標記格式序列化項目。 如果設定為 false,則屬性的值會變更 (不論項目是否具有內容);如果項目是空的,則會以長格式進行序列化。

這個屬性是文件物件模型的 Microsoft 擴充功能。

範例

下列範例會將內容新增至空白元素。

#using <System.Xml.dll>

using namespace System;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew XmlDocument;
   doc->LoadXml( "<book>  <title>Pride And Prejudice</title>  <price/></book>" );
   XmlElement^ currNode = dynamic_cast<XmlElement^>(doc->DocumentElement->LastChild);
   if ( currNode->IsEmpty )
      currNode->InnerXml = "19.95";

   Console::WriteLine( "Display the modified XML..." );
   Console::WriteLine( doc->OuterXml );
}
using System;
using System.Xml;

public class Sample {

  public static void Main() {

      XmlDocument doc = new XmlDocument();
      doc.LoadXml("<book>"+
                  "  <title>Pride And Prejudice</title>" +
                  "  <price/>" +
                  "</book>");

      XmlElement currNode = (XmlElement) doc.DocumentElement.LastChild;
      if (currNode.IsEmpty)
        currNode.InnerXml="19.95";

      Console.WriteLine("Display the modified XML...");
      Console.WriteLine(doc.OuterXml);
  }
}
 Imports System.Xml

public class Sample

  public shared sub Main()
  
      Dim doc as XmlDocument = new XmlDocument()
      doc.LoadXml("<book>" & _
                  "  <title>Pride And Prejudice</title>" & _
                  "  <price/>" & _
                  "</book>")   

      Dim currNode as XmlElement 
      currNode = CType (doc.DocumentElement.LastChild, XmlElement)
      if (currNode.IsEmpty)
        currNode.InnerXml="19.95"
      end if

      Console.WriteLine("Display the modified XML...")
      Console.WriteLine(doc.OuterXml)

  end sub
end class

備註

此屬性是檔物件模型 (DOM) 的 Microsoft 延伸模組。

適用於