Export (0) Print
Expand All

XmlSignificantWhitespace.NodeType Property

Gets the type of the current node.

Namespace: System.Xml
Assembly: System.Xml (in system.xml.dll)

public override XmlNodeType NodeType { get; }
/** @property */
public XmlNodeType get_NodeType ()

public override function get NodeType () : XmlNodeType

Property Value

For XmlSignificantWhitespace nodes, this value is XmlNodeType.SignificantWhitespace.

The following example adds significant white space to the document.

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;
   }
}

import System.*;
import System.IO.*;
import System.Xml.*;

public class Sample
{  
    private XmlNode currNode;
    private String fileName = "space.xml";
    private XmlTextReader reader = null;

    public static void main(String[] args)
    {
        Sample test = new Sample();
    } //main 

    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.get_DocumentElement().get_InnerText());

        // Add white space.   
        currNode = doc.get_DocumentElement();
        XmlSignificantWhitespace sigws = doc.CreateSignificantWhitespace("\t");
        currNode.InsertAfter(sigws, currNode.get_FirstChild());

        Console.WriteLine();
        Console.WriteLine("InnerText after...");
        Console.WriteLine(doc.get_DocumentElement().get_InnerText());

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

    // 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.get_NodeType());
                // Print the node type, name, and value.
                Console.WriteLine("{0}<{1}> {2}", sNodeType, reader.get_Name(),
                reader.get_Value());
            }
        }
        finally {
            if ( reader != null ) {
                reader.Close();
            }
        }
    } //ReadFile

  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;
  } //NodeTypeToString
} //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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft