Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

XmlReader.ReadElementString Method

This is a helper method for reading simple text-only elements.

Overload List

Reads a text-only element.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function ReadElementString() As String
[C#] public virtual string ReadElementString();
[C++] public: virtual String* ReadElementString();
[JScript] public function ReadElementString() : String;

Checks that the Name property of the element found matches the given string before reading a text-only element.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function ReadElementString(String) As String
[C#] public virtual string ReadElementString(string);
[C++] public: virtual String* ReadElementString(String*);
[JScript] public function ReadElementString(String) : String;

Checks that the LocalName and NamespaceURI properties of the element found matches the given strings before reading a text-only element.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function ReadElementString(String, String) As String
[C#] public virtual string ReadElementString(string, string);
[C++] public: virtual String* ReadElementString(String*, String*);
[JScript] public function ReadElementString(String, String) : String;

Example

[Visual Basic, C#, C++] The following example uses ReadElementString to read the contents of the element nodes.

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

public class Sample

  private const filename as string = "book.xml"

  public shared Sub Main()

    Dim reader as XmlTextReader = Nothing

    try
       
      'Load the file and ignore all whitespace.
      reader = new XmlTextReader(filename)
      reader.WhitespaceHandling = WhitespaceHandling.None

      'Moves the reader to the root element.
      reader.MoveToContent()

      'Read the title and price elements.
      Console.WriteLine("Content of the title element: {0}", reader.ReadElementString())
      Console.WriteLine("Content of the price element: {0}", reader.ReadElementString())

    finally
      if Not reader Is Nothing
        reader.Close()
      End if
    End try

  End Sub
End class

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

public class Sample
{
  private const String filename = "book.xml";

  public static void Main()
  {
    XmlTextReader reader = null;

    try
    {           
      //Load the file and ignore all whitespace.
      reader = new XmlTextReader(filename);
      reader.WhitespaceHandling = WhitespaceHandling.None;

      //Moves the reader to the root element.
      reader.MoveToContent();

      //Read the title and price elements.
      Console.WriteLine("Content of the title element: {0}", reader.ReadElementString());
      Console.WriteLine("Content of the price element: {0}", reader.ReadElementString());; 
         
    }
    finally
    {
      if (reader!=null)
        reader.Close();
    }
  }
} // End class

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


int main()
{
   String* filename = S"book.xml";
   XmlTextReader* reader = 0;

   try
   {           
      //Load the file and ignore all whitespace.
      reader = new XmlTextReader(filename);
      reader->WhitespaceHandling = WhitespaceHandling::None;

      //Moves the reader to the root element.
      reader->MoveToContent();

      //Read the title and price elements.
      Console::WriteLine(S"Content of the title element: {0}", reader->ReadElementString());
      Console::WriteLine(S"Content of the price element: {0}", reader->ReadElementString());

   }
   __finally
   {
      if (reader!=0)
         reader->Close();
   }
}

The example uses the data file book.xml:

<!--sample XML fragment-->
<book genre='novel' ISBN='1-861003-78' misc='sale-item'>
  <title>The Handmaid's Tale</title>
  <price>14.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

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft