XmlDocument.CreateWhitespace(String) 方法

定义

创建一个 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

此字符串只能包含下列字符:&#20、&#10、&#13 和 &#9The 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.

适用于