This documentation is archived and is not being maintained.

XmlReader.ReadStartElement Method

Checks that the current node is an element and advances the reader to the next node.

Overload List

Checks that the current node is an element and advances the reader to the next node.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Sub ReadStartElement()
[C#] public virtual void ReadStartElement();
[C++] public: virtual void ReadStartElement();
[JScript] public function ReadStartElement();

Checks that the current content node is an element with the given Name and advances the reader to the next node.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Sub ReadStartElement(String)
[C#] public virtual void ReadStartElement(string);
[C++] public: virtual void ReadStartElement(String*);
[JScript] public function ReadStartElement(String);

Checks that the current content node is an element with the given LocalName and NamespaceURI and advances the reader to the next node.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Sub ReadStartElement(String, String)
[C#] public virtual void ReadStartElement(string, string);
[C++] public: virtual void ReadStartElement(String*, String*);
[JScript] public function ReadStartElement(String, String);

Example

[Visual Basic, C#, C++] The following example parses an XML document using the ReadStartElement and ReadEndElement methods.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of ReadStartElement. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.IO
Imports System.Xml

public class Sample 

  public shared sub Main()

     'Create the reader.
     Dim reader as XmlTextReader = new XmlTextReader("book3.xml")

     'Parse the XML document.  ReadString is used to 
     'read the text content of the elements.
     reader.Read() 
     reader.ReadStartElement("book")  
     reader.ReadStartElement("title")   
     Console.Write("The content of the title element:  ")
     Console.WriteLine(reader.ReadString())
     reader.ReadEndElement()
     reader.ReadStartElement("price")
     Console.Write("The content of the price element:  ")
     Console.WriteLine(reader.ReadString())
     reader.ReadEndElement()
     reader.ReadEndElement()

     'Close the reader.
     reader.Close()
     
  end sub
end class

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

public class Sample 
{
  public static void Main()
  {

     //Create the reader.
     XmlTextReader  reader = new XmlTextReader("book3.xml");

     //Parse the XML document.  ReadString is used to 
     //read the text content of the elements.
     reader.Read(); 
     reader.ReadStartElement("book");  
     reader.ReadStartElement("title");   
     Console.Write("The content of the title element:  ");
     Console.WriteLine(reader.ReadString());
     reader.ReadEndElement();
     reader.ReadStartElement("price");
     Console.Write("The content of the price element:  ");
     Console.WriteLine(reader.ReadString());
     reader.ReadEndElement();
     reader.ReadEndElement();

     //Close the reader.
     reader.Close();
     
  }
} // End class

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

int main()
{
   // Create the reader.
   XmlTextReader* reader = new XmlTextReader(S"book3.xml");

   // Parse the XML document.  ReadString is used to 
   // read the text content of the elements.
   reader -> Read(); 
   reader -> ReadStartElement(S"book");  
   reader -> ReadStartElement(S"title");   
   Console::Write(S"The content of the title element:  ");
   Console::WriteLine(reader -> ReadString());
   reader -> ReadEndElement();
   reader -> ReadStartElement(S"price");
   Console::Write(S"The content of the price element:  ");
   Console::WriteLine(reader -> ReadString());
   reader -> ReadEndElement();
   reader -> ReadEndElement();

   // Close the reader.
   reader -> Close();     
}

The example uses the file, book3.xml, as input.

<book>
  <title>Pride And Prejudice</title>
  <price>19.95</price>
</book>

[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.

See Also

XmlReader Class | XmlReader Members | System.Xml Namespace

Show: