Export (0) Print
Expand All

XmlSignificantWhitespace.NodeType Property

Gets the type of the current node.

[Visual Basic]
Overrides Public ReadOnly Property NodeType As XmlNodeType
[C#]
public override XmlNodeType NodeType {get;}
[C++]
public: __property XmlNodeType get_NodeType();
[JScript]
public override function get NodeType() : XmlNodeType;

Property Value

For XmlSignificantWhitespace nodes, this value is XmlNodeType.SignificantWhitespace.

Example

[Visual Basic, C#, C++] The following example adds significant white space to the document.

[Visual Basic] 
Option Explicit
Option Strict

Imports System
Imports System.IO
Imports System.Xml
Imports Microsoft.VisualBasic

Public Class Sample
    Private currNode As XmlNode
    Private filename As String = "space.xml"
    Private reader As XmlTextReader = Nothing
    
    Public Shared Sub Main()
        Dim test As New Sample()
    End Sub 'Main
    
    Public Sub New()
            Dim doc As New XmlDocument()
            doc.LoadXml("<!-- Sample XML fragment -->" & _
                        "<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.     
            currNode = 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)
            
            ' Save and then display the file.
            doc.Save(filename)
            Console.WriteLine()
            Console.WriteLine("Reading file...")
            ReadFile(filename)
        
    End Sub 'New
     
    
    ' Parse the file and print out each node.
    Public Sub ReadFile(filename As String)
        Try
            reader = New XmlTextReader(filename)
            Dim sNodeType As String = Nothing
            While reader.Read()
                sNodeType = NodeTypeToString(reader.NodeType)
                
                'Print the node type, name, value
                Console.WriteLine("{0}<{1}> {2}", sNodeType, reader.Name, reader.Value)
            End While
        Finally
            If Not (reader Is Nothing) Then
                reader.Close()
            End If
        End Try
    End Sub 'ReadFile
     
    Public Shared Function NodeTypeToString(nodetype As XmlNodeType) As String
        Dim sNodeType As String = Nothing
        Select Case nodetype
            Case XmlNodeType.None
                sNodeType = "None"
            Case XmlNodeType.Element
                sNodeType = "Element"
            Case XmlNodeType.Attribute
                sNodeType = "Attribute"
            Case XmlNodeType.Text
                sNodeType = "Text"
            Case XmlNodeType.Comment
                sNodeType = "Comment"
            Case XmlNodeType.Document
                sNodeType = "Document"
            Case XmlNodeType.Whitespace
                sNodeType = "Whitespace"
            Case XmlNodeType.SignificantWhitespace
                sNodeType = "SignificantWhitespace"
            Case XmlNodeType.EndElement
                sNodeType = "EndElement"
        End Select
        Return sNodeType
    End Function 'NodeTypeToString
End Class 'Sample

[C#] 
using System;
using System.IO;
using System.Xml;

public class Sample {

  private XmlNode currNode;
  private const String filename="space.xml";
  XmlTextReader reader=null;
  
  public static void Main() {
  
    Sample test = new Sample();
  }
  public Sample() {
  
      XmlDocument doc = new XmlDocument();
      doc.LoadXml("<!-- Sample XML fragment -->" +
                  "<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.     
      currNode=doc.DocumentElement;
      XmlSignificantWhitespace sigws=doc.CreateSignificantWhitespace("\t");
      currNode.InsertAfter(sigws, currNode.FirstChild);

      Console.WriteLine();
      Console.WriteLine("InnerText after...");
      Console.WriteLine(doc.DocumentElement.InnerText);

      // Save and then display the file.
      doc.Save(filename);
      Console.WriteLine();
      Console.WriteLine("Reading file...");
      ReadFile(filename);

  }

  // Parse the file and print out each node.
  public void ReadFile(String filename) {
    try {
    
      reader = new XmlTextReader(filename);
      String sNodeType = null;
      while (reader.Read()) {       
       sNodeType = NodeTypeToString(reader.NodeType); 
       // Print the node type, name, and value.
       Console.WriteLine("{0}<{1}> {2}", sNodeType, reader.Name, reader.Value);
      }
    }
    finally {
       if (reader != null)
         reader.Close();
    }
   }

   public static String NodeTypeToString(XmlNodeType nodetype) {
      String sNodeType = null;
      switch (nodetype) {
        case XmlNodeType.None:
          sNodeType = "None";
          break;
        case XmlNodeType.Element:
          sNodeType = "Element";
          break;
        case XmlNodeType.Attribute:
          sNodeType = "Attribute";
          break;
        case XmlNodeType.Text:
          sNodeType = "Text";
          break;
        case XmlNodeType.Comment:
          sNodeType = "Comment";
          break;
        case XmlNodeType.Document:
          sNodeType = "Document";
          break;
        case XmlNodeType.Whitespace:
          sNodeType = "Whitespace";
          break;
        case XmlNodeType.SignificantWhitespace:
          sNodeType = "SignificantWhitespace";
          break;
        case XmlNodeType.EndElement:
          sNodeType = "EndElement";
          break;
      }
      return sNodeType;
   }
}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;

public __gc class Sample {

private:
   XmlNode* currNode;
   XmlTextReader* reader;

public:
   Sample() {
      reader=0;
      String* filename=S"space.xml";   
      XmlDocument* doc = new XmlDocument();
      doc->LoadXml(S"<!-- Sample XML fragment -->" 
                   S"<author xml:space='preserve'>" 
                   S"<first-name>Eva</first-name>"
                   S"<last-name>Corets</last-name>"  
                   S"</author>"); 

      Console::WriteLine(S"InnerText before...");
      Console::WriteLine(doc->DocumentElement->InnerText);

      // Add white space.     
      currNode=doc->DocumentElement;
      XmlSignificantWhitespace* sigws=doc->CreateSignificantWhitespace(S"\t");
      currNode->InsertAfter(sigws, currNode->FirstChild);

      Console::WriteLine();
      Console::WriteLine(S"InnerText after...");
      Console::WriteLine(doc->DocumentElement->InnerText);

      // Save and then display the file.
      doc->Save(filename);
      Console::WriteLine();
      Console::WriteLine(S"Reading file...");
      ReadFile(filename);

   }

   // Parse the file and print out each node.
   void ReadFile(String* filename) {
      try {

         reader = new XmlTextReader(filename);
         String* sNodeType = 0;
         while (reader->Read()) {       
            sNodeType = NodeTypeToString(reader->NodeType); 
            // Print the node type, name, and value.
            Console::WriteLine(S"{0}<{1}> {2}", sNodeType, reader->Name, reader->Value);
         }
      }
      __finally {
         if (reader != 0)
            reader->Close();
      }
   }

   static String* NodeTypeToString(XmlNodeType nodetype) {
      String* sNodeType = 0;
      switch (nodetype) {
         case XmlNodeType::None:
            sNodeType = S"None";
            break;
         case XmlNodeType::Element:
            sNodeType = S"Element";
            break;
         case XmlNodeType::Attribute:
            sNodeType = S"Attribute";
            break;
         case XmlNodeType::Text:
            sNodeType = S"Text";
            break;
         case XmlNodeType::Comment:
            sNodeType = S"Comment";
            break;
         case XmlNodeType::Document:
            sNodeType = S"Document";
            break;
         case XmlNodeType::Whitespace:
            sNodeType = S"Whitespace";
            break;
         case XmlNodeType::SignificantWhitespace:
            sNodeType = S"SignificantWhitespace";
            break;
         case XmlNodeType::EndElement:
            sNodeType = S"EndElement";
            break;
      }
      return sNodeType;
   }
};

int main() {
   new Sample();
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

XmlSignificantWhitespace Class | XmlSignificantWhitespace Members | System.Xml Namespace | XmlNodeType

Show:
© 2014 Microsoft