共用方式為


XmlDocument.CreateSignificantWhitespace(String) 方法

定義

建立 XmlSignificantWhitespace 節點。

public:
 virtual System::Xml::XmlSignificantWhitespace ^ CreateSignificantWhitespace(System::String ^ text);
public virtual System.Xml.XmlSignificantWhitespace CreateSignificantWhitespace (string text);
public virtual System.Xml.XmlSignificantWhitespace CreateSignificantWhitespace (string? text);
abstract member CreateSignificantWhitespace : string -> System.Xml.XmlSignificantWhitespace
override this.CreateSignificantWhitespace : string -> System.Xml.XmlSignificantWhitespace
Public Overridable Function CreateSignificantWhitespace (text As String) As XmlSignificantWhitespace

參數

text
String

字串必須只包含下列字元  和 。

傳回

新的 XmlSignificantWhitespace 節點。

範例

下列範例會將大量的空格元新增至檔。

#using <System.Xml.dll>

using namespace System;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew XmlDocument;
   doc->LoadXml( "<author xml:space='preserve'><first-name>Eva</first-name><last-name>Corets</last-name></author>" );
   Console::WriteLine( "InnerText before..." );
   Console::WriteLine( doc->DocumentElement->InnerText );
   
   // Add white space.     
   XmlNode^ currNode = doc->DocumentElement;
   XmlSignificantWhitespace^ sigws = doc->CreateSignificantWhitespace( "\t" );
   currNode->InsertAfter( sigws, currNode->FirstChild );
   Console::WriteLine();
   Console::WriteLine( "InnerText after..." );
   Console::WriteLine( doc->DocumentElement->InnerText );
}

using System;
using System.Xml;

public class Sample {

  public static void Main() {

      XmlDocument doc = new XmlDocument();
      doc.LoadXml("<author xml:space='preserve'>" +
                  "<first-name>Eva</first-name>"+
                  "<last-name>Corets</last-name>" +
                  "</author>");

      Console.WriteLine("InnerText before...");
      Console.WriteLine(doc.DocumentElement.InnerText);

      // Add white space.
      XmlNode currNode = doc.DocumentElement;
      XmlSignificantWhitespace sigws = doc.CreateSignificantWhitespace("\t");
      currNode.InsertAfter(sigws, currNode.FirstChild);

      Console.WriteLine();
      Console.WriteLine("InnerText after...");
      Console.WriteLine(doc.DocumentElement.InnerText);
  }
}
Option Explicit
Option Strict

Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        
        Dim doc As New XmlDocument()
        doc.LoadXml("<author xml:space='preserve'>" & _
                    "<first-name>Eva</first-name>" & _
                    "<last-name>Corets</last-name>" & _
                    "</author>")
            
        Console.WriteLine("InnerText before...")
        Console.WriteLine(doc.DocumentElement.InnerText)
            
        ' Add white space.    
        Dim currNode as XmlNode = doc.DocumentElement
        Dim sigws As XmlSignificantWhitespace = doc.CreateSignificantWhitespace(ControlChars.Tab)
        currNode.InsertAfter(sigws, currNode.FirstChild)
            
        Console.WriteLine()
        Console.WriteLine("InnerText after...")
        Console.WriteLine(doc.DocumentElement.InnerText)
        
    End Sub 
End Class

備註

此方法是文件物件模型 (DOM) 的 Microsoft 延伸模組。 當您想要手動格式化檔時,會使用它。

雖然這個方法會在文件的內容中建立新的物件,但它不會自動將新物件新增至檔樹狀結構。 若要新增物件,您必須明確呼叫其中一個節點插入方法。

適用於