XmlDocument.CreateWhitespace 方法

创建一个 XmlWhitespace 节点。

**命名空间:**System.Xml
**程序集:**System.Xml(在 system.xml.dll 中)

语法

声明
Public Overridable Function CreateWhitespace ( _
    text As String _
) As XmlWhitespace
用法
Dim instance As XmlDocument
Dim text As String
Dim returnValue As XmlWhitespace

returnValue = instance.CreateWhitespace(text)
public virtual XmlWhitespace CreateWhitespace (
    string text
)
public:
virtual XmlWhitespace^ CreateWhitespace (
    String^ text
)
public XmlWhitespace CreateWhitespace (
    String text
)
public function CreateWhitespace (
    text : String
) : XmlWhitespace

参数

  • text
    此字符串必须只包含下列字符:、
、
 和 	

返回值

一个新的 XmlWhitespace 节点。

备注

该方法是文档对象模型 (DOM) 的 Microsoft 扩展。当希望手动格式化文档时使用它。

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

示例

下面的示例将空白添加到文档中。

Option Explicit
Option Strict

Imports System
Imports System.Xml
Imports Microsoft.VisualBasic

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 'Sample
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);

  } 
}
#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 );
}
import System.*;
import System.Xml.*;

public class Sample
{
    public static void main(String[] args)
    {
        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.get_DocumentElement().get_InnerText());

        // Add white space.     
        XmlNode currNode = doc.get_DocumentElement();
        XmlWhitespace ws = doc.CreateWhitespace("\r\n");
        currNode.InsertAfter(ws, currNode.get_FirstChild());

        Console.WriteLine();
        Console.WriteLine("InnerText after...");
        Console.WriteLine(doc.get_DocumentElement().get_InnerText());
    } //main
} //Sample

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

XmlDocument 类
XmlDocument 成员
System.Xml 命名空间