Reading Element and Attribute Content

To read content of elements and attributes in one call, the XmlReader provides ReadInnerXml and ReadOuterXml methods. When positioned on a node, the ReadInnerXml property pulls in all content from the node, including markup, starting after the element tag up to the element end tag. After the ReadInnerXml call, the XmlReader is positioned after the end element tag. Assuming the following XML input:

Input

<Book>
 <Price Retail="29.95" />
 <Title BookTitle="My First Book" author="me" />
</Book>

This is the output that is returned as a result from calling ReadInnerXml when the reader is positioned on the Book element:

 <Price Retail="29.95" />
 <Title BookTitle="My First Book" author="me" />

ReadInnerXml positions the reader after the end tag of the element being read. That means that for the input above, the current node is the node after the </Book> end element, assuming one exists. Doing a Read method would then advance it an additional element. The following code sample shows the current node in the stream after the ReadInnerXml is called, and how executing a Read after the ReadInnerXml advances the reader yet another node again.

public static void Main() 
{
    string text = @"<book><one><title>ABC</title></one><two><price>24.95</price></two></book>";
    XmlTextReader reader = new XmlTextReader(new StringReader(text));
    reader.WhitespaceHandling = WhitespaceHandling.None;

    // Moves the reader to the 'book' node.
    reader.MoveToContent(); 

    // Moves the reader to the 'one' node.
    reader.Read(); 
    reader.MoveToContent();

    // Reads the inner XML of the 'one' node, which is the entire 'title'
    // node, and outputs <title>ABC</title>. 
    Console.WriteLine(reader.ReadInnerXml()); 

    // The reader is now positioned on the 'two' node.
    Console.WriteLine(reader.Name);

    // Calling Read will advance the reader beyond the 'two' node to 
    // the 'price' node.
    reader.Read();
    // Outputs the node of 'price'.
    Console.WriteLine(reader.Name);
}

The output from the code is:

<title>ABC</title>

two

price

The ReadInnerXml parses XML and returns the content, depending on the XmlNodeType. The following table shows sample content for an element and attribute, the values returned from a call to ReadInnerXml, and the position of the reader after the call.

Node type Child content Return value Position
Element <myelem> text </myelem> text On the node after the ending element </myelem>
Attribute <myelem attr1="val1" attr2="val2"> text </myelem> val1 Remains on the attribute node "attr1".

All other XmlNodeTypes return string.Empty.

The ReadOuterXml method is similar in behavior to the ReadInnerXml method with the exception that its return value includes the start and end tag. When positioned on a leaf node, this method is identical to performing a Read operation.

The ReadOuterXml parses XML and returns various child content, depending on the current XmlNodeType. The following table shows sample content for an element and attribute, the values returned from a call to ReadOuterXml, and the position of the reader after the call.

Node type Child content Return value Position
Element <elem1> text </elem1> <elem1> text </elem1> Positioned after the </elem1> tag.
Attribute <elem1 attr1="attrValue1" attr2="attrValue2" > text </elem1> attr1="attrValue1" Positioned on the attribute node "attr1".

All other node types return string.Empty to the ReadOuterXml.

See Also

Reading XML with the XmlReader | Current Node Position in XmlReader | Property Settings on XmlReader | Object Comparison Using XmlNameTable with XmlReader | Reading Attributes with XmlReader | Skipping Content with XmlReader | EntityReference Reading and Expansion | Comparing XmlReader to SAX Reader | Reading XML Data with XmlTextReader | Reading Node Trees with XmlNodeReader | Validating XML with XmlValidatingReader | Customized XML Reader Creation | XmlReader Class | XmlReader Members | XmlNodeReader Class | XmlNodeReader Members | XmlTextReader Class | XmlTextReader Members | XmlValidatingReader Class | XmlValidatingReader