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.ReadInnerXml Method

When overridden in a derived class, reads all the content, including markup, as a string.

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

Return Value

All the XML content, including markup, in the current node. If the current node has no children, an empty string is returned.

If the current node is neither an element nor attribute, an empty string is returned.

Exceptions

Exception Type Condition
XmlException The XML was not well-formed, or an error occurred while parsing the XML.

Remarks

This method returns all the content of the current node including the markup. The current node (start tag) and corresponding end node (end tag) are not returned. For example, if you had the following:

<node>
 this <child id="123"/>
</node>
   

ReadInnerXml returns this <child id="123"/>

This method handles element and attribute nodes in the following manner:

Node Type Position Before the Call XML Fragment Return Value Position After the Call
Element On the item1 start tag. <item1>text1</item1><item2>text2</item2> text1 On the item2 start tag.
Attribute On the attr1 attribute node. <item attr1="val1" attr2="val2">text</item> val1 Remains on the attr1 attribute node.

If the reader is positioned on a leaf node, calling ReadInnerXml is equivalent to calling Read.

This method checks for well-formed XML. If ReadInnerXml is called from an XmlValidatingReader, this method also validates the content returned.

As implemented in the XmlNodeReader, XmlTextReader and XmlValidatingReader classes the ReadInnerXml method is namespace aware.

Example

[Visual Basic, C#, C++] The following example compares the ReadInnerXml and ReadOuterXml methods.

[Visual Basic] 
Imports System
Imports System.IO
Imports System.Xml

'Reads an XML file

public class Sample

  private const filename as string = "2books.xml"

  public shared Sub Main()

    Dim reader as XmlTextReader = Nothing

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

     ' Moves the reader to the root element.
     reader.MoveToContent()
 
     ' Moves to book node.
     reader.Read()

     ' Note that ReadInnerXml only returns the markup of the node's children
     ' so the book's attributes are not returned.
     Console.WriteLine("Read the first book using ReadInnerXml...")
     Console.WriteLine(reader.ReadInnerXml())

     ' ReadOuterXml returns the markup for the current node and its children
     ' so the book's attributes are also returned.
     Console.WriteLine("Read the second book using ReadOuterXml...")
     Console.WriteLine(reader.ReadOuterXml())            
        
    finally
      if Not reader Is Nothing
        reader.Close()
      End if
    End try

  End Sub
End class

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

//Reads an XML fragment

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

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

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

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

       // Note that ReadInnerXml only returns the markup of the node's children
       // so the book's attributes are not returned.
       Console.WriteLine("Read the first book using ReadInnerXml...");
       Console.WriteLine(reader.ReadInnerXml());

       // ReadOuterXml returns the markup for the current node and its children
       // so the book's attributes are also returned.
       Console.WriteLine("Read the second book using ReadOuterXml...");
       Console.WriteLine(reader.ReadOuterXml());                    

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

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

//Reads an XML fragment


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

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

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

      // Moves to book node.
      reader->Read(); 

      // Note that ReadInnerXml only returns the markup of the node's children
      // so the book's attributes are not returned.
      Console::WriteLine(S"Read the first book using ReadInnerXml...");
      Console::WriteLine(reader->ReadInnerXml());

      // ReadOuterXml returns the markup for the current node and its children
      // so the book's attributes are also returned.
      Console::WriteLine(S"Read the second book using ReadOuterXml...");
      Console::WriteLine(reader->ReadOuterXml());                    

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

[Visual Basic, C#, C++] The example uses 2books.xml file as input.

[VB, C#]
<!--sample XML fragment-->
<bookstore>
  <book genre='novel' ISBN='10-861003-324'>
    <title>The Handmaid's Tale</title>
    <price>19.95</price>
  </book>
  <book genre='novel' ISBN='1-861001-57-5'>
    <title>Pride And Prejudice</title>
    <price>24.95</price>
  </book>
</bookstore>

[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, Common Language Infrastructure (CLI) Standard

See Also

XmlReader Class | XmlReader Members | System.Xml Namespace

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