Language

XmlTextReader.Name Property

Definition

Gets the qualified name of the current node.

public:
 virtual property System::String ^ Name { System::String ^ get(); };
public override string Name { get; }
member this.Name : string
Public Overrides ReadOnly Property Name As String

Property Value

The qualified name of the current node. For example, Name is bk:book for the element .

The name returned is dependent on the NodeType of the node. The following node types return the listed values. All other node types return an empty string.

Node Type Name
Attribute The name of the attribute.
DocumentType The document type name.
Element The tag name.
EntityReference The name of the entity referenced.
ProcessingInstruction The target of the processing instruction.
XmlDeclaration The literal string xml.

Examples

The following example reads an XML file and displays each of the nodes.

using System;
using System.IO;
using System.Xml;

public class Sample {

  private const String filename = "items.xml";

  public static void Main() {

     XmlTextReader reader = null;

     try {

        // Load the reader with the data file and ignore all white space nodes.
        reader = new XmlTextReader(filename);
        reader.WhitespaceHandling = WhitespaceHandling.None;

        // Parse the file and display each of the nodes.
        while (reader.Read()) {
           switch (reader.NodeType) {
             case XmlNodeType.Element:
               Console.Write("<{0}>", reader.Name);
               break;
             case XmlNodeType.Text:
               Console.Write(reader.Value);
               break;
             case XmlNodeType.CDATA:
               Console.Write("", reader.Value);
               break;
             case XmlNodeType.ProcessingInstruction:
               Console.Write("", reader.Name, reader.Value);
               break;
             case XmlNodeType.Comment:
               Console.Write("", reader.Value);
               break;
             case XmlNodeType.XmlDeclaration:
               Console.Write("");
               break;
             case XmlNodeType.Document:
               break;
             case XmlNodeType.DocumentType:
               Console.Write("", reader.Name);
               break;
           }
        }
     }

     finally {
        if (reader!=null)
          reader.Close();
     }
  }
} // End class
Option Strict
Option Explicit

Imports System.IO
Imports System.Xml

'Reads an XML document
Public Class Sample
    Private Const filename As String = "items.xml"
    
    Public Shared Sub Main()
        Dim reader As XmlTextReader = Nothing
        
        Try
            ' Load the reader with the data file and ignore all white space nodes.         
            reader = New XmlTextReader(filename)
            reader.WhitespaceHandling = WhitespaceHandling.None
            
            ' Parse the file and display each of the nodes.
            While reader.Read()
                Select Case reader.NodeType
                    Case XmlNodeType.Element
                        Console.Write("<{0}>", reader.Name)
                    Case XmlNodeType.Text
                        Console.Write(reader.Value)
                    Case XmlNodeType.CDATA
                        Console.Write("", reader.Value)
                    Case XmlNodeType.ProcessingInstruction
                        Console.Write("", reader.Name, reader.Value)
                    Case XmlNodeType.Comment
                        Console.Write("", reader.Value)
                    Case XmlNodeType.XmlDeclaration
                        Console.Write("")
                    Case XmlNodeType.Document
                    Case XmlNodeType.DocumentType
                        Console.Write("", reader.Name)
                End Select
            End While
        
        Finally
            If Not (reader Is Nothing) Then
                reader.Close()
            End If
        End Try
    End Sub
End Class

The sample uses the file items.xml.




]>

  Test with an entity: &number;
  test with a child element  stuff
  test with a CDATA section ]]> def
  Test with an char entity: A
  
  1234567890ABCD

Remarks

Note

We recommend that you create XmlReader instances by using the XmlReader.Create method to take advantage of new functionality.

Applies to

See also