XmlDocument.CreateWhitespace(String) XmlDocument.CreateWhitespace(String) XmlDocument.CreateWhitespace(String) XmlDocument.CreateWhitespace(String) Method

定義

建立 XmlWhitespace 節點。Creates an XmlWhitespace node.

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

參數

text
String String String String

字串必須只包含下列字元: 
 
 和 	The string must contain only the following characters  
 
 and 	

傳回

新的 XmlWhitespace 節點。A new XmlWhitespace node.

範例

下列範例會在檔中加上空白字元。The following example adds white space to the document.

#using <System.Xml.dll>

using namespace System;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew XmlDocument;
   doc->LoadXml( "<author><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;
   XmlWhitespace^ ws = doc->CreateWhitespace( "\r\n" );
   currNode->InsertAfter( ws, 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>" +
                  "<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;
      XmlWhitespace ws = doc.CreateWhitespace("\r\n");
      currNode.InsertAfter(ws, 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>" & _
                    "<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 ws As XmlWhitespace = doc.CreateWhitespace(ControlChars.CrLf)
        currNode.InsertAfter(ws, currNode.FirstChild)
            
        Console.WriteLine()
        Console.WriteLine("InnerText after...")
        Console.WriteLine(doc.DocumentElement.InnerText)
        
    End Sub 
End Class

備註

這個方法是檔物件模型 (DOM) 的 Microsoft 擴充功能。This method is a Microsoft extension to the Document Object Model (DOM). 當您想要手動格式化檔時, 會使用它。It is used when you want to manually format your document.

雖然這個方法會在檔的內容中建立新的物件, 但它不會自動將新的物件加入至檔樹狀結構。Although this method creates the new object in the context of the document, it does not automatically add the new object to the document tree. 若要加入新的物件, 您必須明確地呼叫其中一個節點插入方法。To add the new object, you must explicitly call one of the node insert methods.

適用於