Export (0) Print
Expand All

XmlReader.ReadOuterXml Method

When overridden in a derived class, reads the content, including markup, representing this node and all its children.

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

Return Value

If the reader is positioned on an element or an attribute node, this method returns all the XML content, including markup, of the current node and all its children; otherwise, it returns an empty string.

Exceptions

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

Remarks

This method is similar to ReadInnerXml except it also returns the start and end tags.

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> <item1>text1</item1> On the item2 start tag.
Attribute On the attr1 attribute node. <item attr1="val1" attr2="val2">text</item> attr1="val1" Remains on the attr1 attribute node.

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

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

As implemented in the XmlNodeReader, XmlTextReader and XmlValidatingReader classes the ReadOuterXml method is namespace aware. Given the following XML text <A

xmlns:S="urn:1"><S:B>hello</S:B></A>, if the reader were positioned on the S:B start tag, ReadOuterXml returns <S:B

xmlns:S="urn:1">hello<S:B/>.

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

Show:
© 2014 Microsoft