Redigera

Dela via


XmlDocument.CreateWhitespace(String) Method

Definition

Creates an XmlWhitespace node.

public:
 virtual System::Xml::XmlWhitespace ^ CreateWhitespace(System::String ^ text);
public virtual System.Xml.XmlWhitespace CreateWhitespace (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

Parameters

text
String

The string must contain only the following characters  
 
 and 	.

Returns

A new XmlWhitespace node.

Examples

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

Remarks

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.

Applies to