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

注解

此方法是文档对象模型的 Microsoft 扩展, (DOM) 。 当您要手动设置文档格式时,会使用它。

尽管此方法在文档的上下文中创建新对象,但它不会自动将新对象添加到文档树。 若要添加新对象,必须显式调用节点插入方法之一。

适用于